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()
3 7 0 9 8
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.
This article is contributed by Yash Singla. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. 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.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Linear Congruence method for generating Pseudo Random Numbers
- Multiplicative Congruence method for generating Pseudo Random Numbers
- Additive Congruence method for generating Pseudo Random Numbers
- Random number generator in arbitrary probability distribution fashion
- Nambiar Number Generator
- Select a random number from stream, with O(1) space
- Probability of getting a perfect square when a random number is chosen in a given range
- Random list of M non-negative integers whose sum is N
- Binomial Random Variables
- Erdos Renyl Model (for generating Random Graphs)
- Probability of getting two consecutive heads after choosing a random coin among two different types of coins
- Generate a random permutation of elements from range [L, R] (Divide and Conquer)
- Probability that a random pair chosen from an array (a[i], a[j]) has the maximum sum
- Minimum size binary string required such that probability of deleting two 1's at random is 1/X
- N-Queen Problem | Local Search using Hill climbing with random neighbour
- Number of factors of very large number N modulo M where M is any prime number
- Find minimum number to be divided to make a number a perfect square
- How to check if a given number is Fibonacci number?
- Find the smallest number whose digits multiply to a given number n
- Find n'th number in a number system with only 3 and 4
Improved By : etrigan76