Languages
[Edit]
EN

TypeScript - own random number generator (custom implementation)

3 points
Created by:
Palus-Bear
1016

In this article, we're going to have a look at how to write own random numbers generator that is based on LCG (Linear congruential generator) algorithm in TypeScript.

1. Introduction

Edit

In TypeScript, we can implement a custom random number generator by using LCG (Linear congruential generator) algorithm. LCG is one of the oldest and best-known pseudorandom number generator algorithm. We can adjust this implementation to work on int or long.

The linear congruential generator is defined by the recurrence formula:

Where:

  • Xn+1 - new seed,
  • a - multiplier,
  • Xn - current seed (comes from previous calculations),
  • c - incrementer,
  • m - modulus (max integer value).

2. Implementation

Edit

Note: every time randomInt() method is called, it is necessary to update seed value.

Alternative titles

  1. LCG (Linear congruential generator) algorithm implementation in TypeScript
  2. TypeScript - random numbers algorithm
1
Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.
Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join