๐—›๐—ผ๐˜„ ๐—ฑ๐—ผ ๐˜†๐—ผ๐˜‚ ๐—บ๐—ฎ๐—ธ๐—ฒ ๐—ฎ ๐—ฐ๐—ผ๐—บ๐—ฝ๐˜‚๐˜๐—ฒ๐—ฟ ๐—ด๐—ฒ๐—ป๐—ฒ๐—ฟ๐—ฎ๐˜๐—ฒ ๐—ฟ๐—ฎ๐—ป๐—ฑ๐—ผ๐—บ ๐—ป๐˜‚๐—บ๐—ฏ๐—ฒ๐—ฟ๐˜€๏ผŸ
For example, You have a list of numbers, say [1,2,3,4,5,6,7,8,9,10]
What program would you write to pick a random number from the list without using any inbuilt module?
Try it out now!
You may have noticed that you need a random index to get a random number from the list! You may be surprised to hear that computers are very bad at generating truly random numbers because of their deterministic nature.
What they generate are pseudo-random numbers which means after a long enough time the sequence generated by the computer would eventually repeat itself, I would talk about it in more depth but first, let's talk about true random numbers.
True Random Number Generators (TRNGs)

Computers can generate truly random numbers by collecting information from unpredictable environmental conditions like mouse movements, fan noise, time.
For example: if you wanted to generate an integer value between 1 and 10, and you press the enter key at exactly 7:57:55.287503482 PM , then the computer might take that last digit and use โ€œ2โ€ as output.
Interesting isn't it?
Pseudo-Random Number Generators
A pseudo-random process is a process that appears to be random but is not. Pseudo-random sequences typically exhibit statistical randomness while being generated by an entirely deterministic process.
We are going to talk about one of the algorithms used to generate pseudo-random numbers -- Linear Congruential generator.
Let's think of any four numbers:
Xโ‚™ -> is the start value of seed

๐š -> number which is going to be multiplied with Xโ‚™

c -> number which is going to be added to aXโ‚™

m -> the number by which we are going to divide the resulting equation (aXโ‚™ + c) to get the remainder.
By putting the numbers defined in the equation, we get a seed, which we can use again in the equation to provide one more seed thus creating a recursion.
By combining the results we get a random sequence of numbers.
Let's try out an example :
Let Xโ‚’(starting number) = 100,
a = 4567, c=7897 and m = 1,000,000

By putting in the equation, we get

Xโ‚ = (aXโ‚’ + c)mod m
= (4567(100)+ 7897)mod 1,000,000
= 464,597
Then put the result Xโ‚ in the equation again
Xโ‚‚ = (aXโ‚ + c)mod m
= (4567(464597) + 7897)mod 1,000,000
Xโ‚‚ = 822396
By doing 2 steps we have got a random sequence -> 464597,822396.
Repeat the process until the desired length is achieved.
Python code for generating random numbers using Linear Congruential generator (credits: @geeksforgeeks)->
You can follow @gagan45107461.
Tip: mention @twtextapp on a Twitter thread with the keyword โ€œunrollโ€ to get a link to it.

Latest Threads Unrolled: