Building Pseudo Random Number Generator from scratch in JavaScript

Pseudo-Random Number Generator otherwise called PRNG is a concept that involves producing a series of random numbers using mathematical formulas. Generating a random number is not as easy as it looks. When we have an algorithm and set of inputs, we get output. And if we again use the same algorithm with the same set of inputs, we get the same output and not a different one. We cannot produce a truly random number using any algorithm. But we can produce pseudo-random numbers for which the numbers are almost unpredictable that it seems like a random number.

There are several algorithms available to produce PRNG. We will be seeing a couple of basic algorithms to understand the concept and implement them using javascript.

Middle Square Method in JavaScript

This is one of the simplest algorithms to produce a Pseudo-random number. It has the following step.

  • Take a seed value (s), of fixed length/size (n), example, n = 4 and s = 1242
  • Square the value of s, the resultant value will atmost be of length 2 times of n, if not padd 0 to the left of the resultant value, and let’s call it as S. Example sqr(s) = 1542564, S = 01542564
  • Take the middle 4 digits from S. It is the random value obtained using the seed value s. Example, random number = 5425
  • Now use this random number as the new seed value and generate the next random number by following step number 2
  • If you want the number between 0 to 1, divide the resultant number by the maximum number that can be formed using n digits. For example, 5425/9999 = 0.54255425542
function pseudoRandom(seed, num_of_digits){
  n = (seed * seed).toString()
   while(n.length < num_of_digits * 2 ){
    n = "0" + n
  }
  start = Math.floor(num_of_digits / 2)
  end = start + num_of_digits
  seed = parseInt(n.substring(start, end))
  return seed
}

num_of_digits = 4
seed = 1452
for(i = 0; i<5; i++){
  random_number = pseudoRandom(seed, num_of_digits)
  console.log(random_number)
  seed = random_number
}

Output

1083
1728
9859
1998
9920

 

Linear Congruential Generator in JavaScript

Linear congruential generator is a popular PRNG algorithm and it is used in lots of programming languages and systems. It is simple to implement and faster to execute. It is also a way lot better algorithm than Middle Square Method.

Formula :

Random number = ( a*seed + c) mod m

where,
seed, 0 < seed < m is the initial value that is provided to the algorithm,
a, 0 < a < m –  is the multiplier,
c, 0 < c < m – is the increment,
m, 0 < m –  is the modulus.

To produce a random number between 0 and 1, we divide the generated number by m. The random number generated will be used as seed value to generate next random number.

function pseudoRandom(seed, a, c, m){
  seed = (a * seed + c) % m  
  return seed / m
}

seed = 1452
a = 1664525
c = 1013904223
m = Math.pow(2, 32)
for(i = 0; i<5; i++){
  random_number = pseudoRandom(seed, a, c, m)
  console.log(random_number)
  seed = random_number
}

Output

0.7987940970342606
0.23637754743554767
0.23615958153669378
0.23615949706348063
0.23615949703074285

Please post your comments, doubts, queries in the comment box. Happy Learning 🙂

Also, read

Leave a Reply

Your email address will not be published.