How to generate random numbers?(Linear Congruential Generator)

Ishant Jindal
4 min readDec 6, 2021

--

Everyone must have used or generated some random numbers in life to work on, either manually or by using a program. In python there is a popular library name ‘random’ by which you can actually generate random numbers. Ever wondered if a program is actually a set of instructions, how it can generate random numbers or are those numbers really random? Well in this article we will discuss what pseudo-random numbers are, how one can generate pseudo-random numbers and my thoughts on what actually ‘random’ means.

Pseudo-Random Numbers

Numbers or elements which are generated by taking a known starting point and are altered by a value commonly know as seed. These sequences are tend to repeat over time thus are not considered as truly random but are good enough to perform experiments that require random numbers or sets. The recurrence of the same sequence depends on the parameters which are involved in generating these numbers. One of the methods to generate Pseudo-Random Numbers is Linear Congruential Generator(LCG) which comes under the category of PRNG(Pseudo-Random Numbers Generator).

Linear Congruential Generator(LCG)

LCG is considered one of the basic yet best methods to generate Pseudo-Random Numbers. It works on a recurrence relation which is defined as follows

LCG recurrence relation

The distance between recurrence depends on the values of a, c, seed, and m, although ‘m’ plays a major role in the length of a sequence. There are some tested values of seed, a, c and m which generated some large series. You can check those values by clicking here.

Series when ‘m’ is small

If you carefully observe the above graph you can see that the values are getting repeated after a particular gap. Here the seed value is time.time() which returns the current time value and a, c and m are some prime numbers.

But if I increase the value of a, c and m to a large prime number specially m, I will get this graph as an output.

Series when ‘m’ is large

This series in not repeating in the interval of 100, there could be a chance that it can repeat in a interval of 1000 or 10⁶ or even more. Therefore it is necessary to carefully choose the parameters.

So if the series is recurring, you must be wondering these are not random numbers since they can be predicted or repeated over time. The answer to that question is for a good value of seed and m you can generate a series so long that it will be impossible for any human or even regular computers to guess a sequence this large unless they are provided with the seed. So seed plays an important role to decrypt the sequence which is further decrypted by mod m. And if one can get a sequence that is large enough there is no machine that can figure out the seed, m, a and c without brute force. Since the generated series is long and no one can predict the next number without seed thus these numbers are considered pseudo-random numbers.

There are many ways in which one can choose a seed. For instance one can choose time.time() or thermal pressure as a seed. If you’re thinking about whether you can generate a truly random number using a computer I would suggest you to read this.

Here is my code to generate random numbers.

What does Random actually mean?

The English definition of random is ‘choosing by chance’. According to my understanding randomness is something which we humans can’t interpret. Earlier rainfall was considered as a random event but know we can predict rain 3 days before the that day. So this means there will be a day that an event which was considered as random which get predictable. So this means all the results which we humans generated using that phenomenon will be invalid? Well that’s one interesting question to search for.

--

--

Ishant Jindal
Ishant Jindal

Written by Ishant Jindal

A Data science enthusiast who is exploring stuff to keep himself interested in world.

No responses yet