Random Numbers !
One of the objectives of my current project is to make the results obtained from an evolutionary algorithm reproducible. The first thing to do was to extract the current state of the random number generator. I spent few hours reading about true-random numbers, pseudo-random numbers, random number generation algorithms, etc. In this article, I would like to discuss few things that I came across.
- What are random numbers?
- True-random numbers and Pseudo-random numbers
- Random Numbers in Python
- Seed
- Random Number Generator using JavaScript
What are random numbers?
I came across this concept in C++ Programming class in school. A simple google search “Generate Random Number” could explain that to you(as shown below). The system generates a random number in the range specified by the user. Every number has an equal chance of being generated without any bias.
True Random Number and Pseudo-Random Number Generators
TRNGs harvest the source of randomness from some physical phenomenon that is expected to be random (such as system time, radioactive decay process, etc).
PRNGs use computational algorithms to generate random numbers which are not fully random. The results depend upon the initial value called the seed. Hence, knowing the algorithm and the seed one can reproduce these random results. Almost all programming languages can generate only pseudo-random numbers.
Random Numbers in Python
In python, the NumPy.random library has various functions to generate random numbers as per our requirements. Here are few examples:
Seed
A seed is a random number that initializes the pseudo-random number generator. With a particular seed, the same sequence of numbers is produced. Hence, setting the seed can make the results reproducible.
np.random.get_state() and np.random.set_state() can also help us to save or initialize the state of the random number generator in python.
Random Number Generator using JavaScript
In JavaScript, there is no direct function to generate a random integer in a given range. Math.random() returns a random number between 0 (inclusive), and 1 (exclusive). It needs to be manipulated to produce that random integer.
min = 105; max = 1000; => Math.floor(Math.random()*895)+105
CodePen — Random Number Generator by R.K.Athisha
I hope this helps you. Let me know your thoughts/suggestions/feedback in the comments section below. Thank You!