How Computers generate random numbers ?

We have all used .random() function in our programming journey, which returned us some random number in some specified range. Did you ever think how the random number was generated?

You’ll agree that,

“A computer is a machine that can be programmed…


This content originally appeared on DEV Community and was authored by Devesh Anand Srivastava

We have all used .random() function in our programming journey, which returned us some random number in some specified range. Did you ever think how the random number was generated?

You'll agree that,

"A computer is a machine that can be programmed to carry out sequences of arithmetic or logical operations automatically."
- Wikipedia

Then how can a computer be programmed to generate a random number through sequence of operations? If some random number is generated through some sequence of steps (algorithm), is it truly random? This takes us to the types of random numbers there are:

  1. True Random Numbers
  2. Pseudo Random Numbers

1. True Random Numbers:

As the name suggests, they are truly random. Since an algorithmically generated number can't be a random number in true sense, true random numbers are generated using unpredictable data from real world, like the rpm (rotations per minute) of CPU fan, or exact time you press a key on your keyboard, or atmospheric noise around the computer. Such physical phenomenon give completely unpredictable set of entropy (randomness), making true random numbers secure.

Random number generators of this kind are called True random number generators (TRNGs). Linux uses dev/random to collect entropy from atmosphere around you, and creates true random numbers. It gathers entropy in form of environmental noise through device drivers.

2. Pseudo Random Numbers:

These are alternative to true random numbers, generated using some seed value and some algorithm. The numbers generated seem to be random, but are predictable in reality if seed value and algorithm is known.

As you might have already guessed, they are not great from security perspective. This is why using PRNGs in encryption is a bad idea. FreeBSD takes it rather seriously.

Random number generators generating these kinds of numbers are called pseudo random number generators (PRNGs). PRNGs are faster compared to TRNGs, and are useful in scenarios where security is not a concern, like games, or while learning programming.

One of the most common PRNG is linear congruential generator. Let's see how it works.

It uses recurrence:

Xn+1 = (aXn + b) mod m

Where,
X is sequence of pseudo random values
m is the modulus
a the multiplier
c the increment
X0 the seed value

Let's have an example. Taking X0 as 10,
a = 22,
c = 723,
m = 10,000

X1 = (aX0 + c) mod m
X1 = ( 22(10) + 723 )mod 10000
X1 = 943

Now to get another random number X2, put value of X1 from above,

X2 = (aX1 + c) mod m
X2 = ( 22(943) + 743 )mod 10000
X2 = 1489

This is one of the numerous ways how you can generate different (pseudo) random numbers of different sizes.

Javascript code for Linear Congruence method:

// Function to generate random numbers
function linearCongruentialMethod(Xo, m,  a, c,
                     randomNums, noOfRandomNums)
{

    // Initialize the seed state
    randomNums[0] = Xo;

    // Traverse to generate required
    // numbers of random numbers
    for(let i = 1; i < noOfRandomNums; i++)
    {

        // Follow the linear congruential method
        randomNums[i] = ((randomNums[i - 1] * a) + c) % m;
    }
}

    // Driver Code

    // Seed value
    let Xo = 5;   
    // Modulus parameter
    let m = 7;
    // Multiplier term
    let a = 3; 
    // Increment term
    let c = 3; 
    // Number of Random numbers
    // to be generated
    let noOfRandomNums = 10;

    // To store random numbers
    let randomNums = new Array(noOfRandomNums).fill(0);

    // Function Call
    linearCongruentialMethod(Xo, m, a, c,
                             randomNums,
                             noOfRandomNums);

    for(let i = 0; i < noOfRandomNums; i++)
    {
        document.write(randomNums[i] + " ");
    }

Credits: Geeksforgeeks


This content originally appeared on DEV Community and was authored by Devesh Anand Srivastava


Print Share Comment Cite Upload Translate Updates
APA

Devesh Anand Srivastava | Sciencx (2021-05-18T16:16:26+00:00) How Computers generate random numbers ?. Retrieved from https://www.scien.cx/2021/05/18/how-computers-generate-random-numbers/

MLA
" » How Computers generate random numbers ?." Devesh Anand Srivastava | Sciencx - Tuesday May 18, 2021, https://www.scien.cx/2021/05/18/how-computers-generate-random-numbers/
HARVARD
Devesh Anand Srivastava | Sciencx Tuesday May 18, 2021 » How Computers generate random numbers ?., viewed ,<https://www.scien.cx/2021/05/18/how-computers-generate-random-numbers/>
VANCOUVER
Devesh Anand Srivastava | Sciencx - » How Computers generate random numbers ?. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/05/18/how-computers-generate-random-numbers/
CHICAGO
" » How Computers generate random numbers ?." Devesh Anand Srivastava | Sciencx - Accessed . https://www.scien.cx/2021/05/18/how-computers-generate-random-numbers/
IEEE
" » How Computers generate random numbers ?." Devesh Anand Srivastava | Sciencx [Online]. Available: https://www.scien.cx/2021/05/18/how-computers-generate-random-numbers/. [Accessed: ]
rf:citation
» How Computers generate random numbers ? | Devesh Anand Srivastava | Sciencx | https://www.scien.cx/2021/05/18/how-computers-generate-random-numbers/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.