Tutorialspoint.dev

Pseudo Random Number Generator (PRNG)

Pseudo Random Number Generator(PRNG) refers to an algorithm that uses mathematical formulas to produce sequences of random numbers. PRNGs generate a sequence of numbers approximating the properties of random numbers.

A PRNG starts from an arbitrary starting state using a seed state. Many numbers are generated in a short time and can also be reproduced later, if the starting point in the sequence is known. Hence, the numbers are deterministic and efficient.

Why do we need PRNG?

With the advent of computers, programmers recognized the need for a means of introducing randomness into a computer program. However, surprising as it may seem, it is difficult to get a computer to do something by chance as computer follows the given instructions blindly and is therefore completely predictable. It is not possible to generate truly random numbers from deterministic thing like computers so PRNG is a technique developed to generate random numbers using a computer.

How PRNG works?



Linear Congruential Generator is most common and oldest algorithm for generating pseudo-randomized numbers. The generator is defined by the recurrence relation:

Xn+1 = (aXn + c) mod c
where X is the sequence of pseudo-random values
m, 0< m- modulus 
a, 0< a <m- multiplier
c, 0< = c<m- increment
x0, 0<=x0<m- the seed or start value

We generate the next random integer using the previous random integer, the integer constants, and the integer modulus. To get started, the algorithm requires an initial Seed, which must be provided by some means. The appearance of randomness is provided by performing modulo arithmetic..

Characteristics of PRNG

  • Efficient: PRNG can produce many numbers in a short time and is advantageous for applications that need many numbers
  • Deterministic: A given sequence of numbers can be reproduced at a later date if the starting point in the sequence is known.Determinism is handy if you need to replay the same sequence of numbers again at a later stage.
  • Periodic: PRNGs are periodic, which means that the sequence will eventually repeat itself. While periodicity is hardly ever a desirable characteristic, modern PRNGs have a period that is so long that it can be ignored for most practical purposes

Applications of PRNG

PRNGs are suitable for applications where many random numbers are required and where it is useful that the same sequence can be replayed easily. Popular examples of such applications are simulation and modeling applications. PRNGs are not suitable for applications where it is important that the numbers are really unpredictable, such as data encryption and gambling.

Pseudo Random Number Generator using srand()

#include<stdio.h>
#include<stdlib.h>
#include<time.h>
  
int main()
{
   srand(time(NULL));
   int i;
  
   for(i = 0; i<5; i++)
  printf("%d ", rand()%10);
}

Output 1:



3  7  0  9  8

Output 2:

7  6  8  1  4

Explanation: srand() sets the seed which is used by rand() to generate random numbers.time(NULL) return no. of second from JAN 1, 1971 i.e every time we run program we have difference of few seconds which gives the program new seed.

Widely used PRNG algorithms : Lagged Fibonacci generators, linear feedback shift registers, Blum Blum Shub.

Quiz on Random Numbers

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.



This article is attributed to GeeksforGeeks.org

leave a comment

code

0 Comments

load comments

Subscribe to Our Newsletter