Open In App
Related Articles
• Write an Interview Experience
• Mathematical Algorithms

# 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 m
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()

## C++

 `#include``using` `namespace` `std;` `int` `main()``{``    ``srand``(``time``(NULL));``    ``int` `i;``    ``for``(i = 0; i < 5; i++)``        ``std::cout << (``rand``()%10) << ``"\t"``;``    ``return` `0;``}` `// This code is contributed by Srj_27`

## C

 `#include``#include``#include` `int` `main()``{``srand``(``time``(NULL));``int` `i;` `for``(i = 0; i<5; i++)``printf``(``"%d\t"``, ``rand``()%10);``}`

## Java

 `// Java code to implement the approach``import` `java.util.*;` `class` `GFG {``  ``public` `static` `void` `main(String[] args)``  ``{` `    ``// Initializing Random object with``    ``// the current time as seed value``    ``Random rand``      ``= ``new` `Random(System.currentTimeMillis());``    ``int` `i;` `    ``for` `(i = ``0``; i < ``5``; i++)``      ``System.out.print(rand.nextInt(``10``) + ``"\t"``);``  ``}``}` `// This code is contributed by phasing17`

## Python3

 `# Python3 code to implement the``# approach``import` `random``from` `datetime ``import` `datetime` `# Passing the current time as the seed value``random.seed(datetime.now())` `for` `i ``in` `range``(``5``):``    ``print``(random.randint(``0``, ``10``), end``=``"\t"``)` `# This code is contributed by phasing17`

## C#

 `// C# code to implement the approach` `using` `System;` `class` `GFG {``    ``public` `static` `void` `Main(``string``[] args)``    ``{``        ``// Initializing Random object with``        ``// the current time as seed value``        ``Random rand = ``new` `Random(DateTime.Now.Day);``        ``int` `i;` `        ``for` `(i = 0; i < 5; i++)``            ``Console.Write(rand.Next(10) + ``"\t"``);``    ``}``}` `// This code is contributed by phasing17`

## Javascript

 `// JavaScript code to implement the``// approach`  `for``(``var` `i = 0; i<5; i++)``    ``process.stdout.write(Math.floor(Math.random() * 10) + ``"\t"``);`  `// This code is contributed by phasing17`

Output 1:

`3  7  0  9  8`

Output 2:

`7  6  8  1  4`

Time Complexity: O(1)

Auxiliary Space: O(1)

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 This article is contributed by Yash Singla. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.