Random Numbers !

Athisha R K
Analytics Vidhya
Published in
3 min readFeb 26, 2021
Photo by Aaron Sebastian on Unsplash

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.

It is an example of a random number generator on Google. The system has generated 59 as the random number between 10 and 102. The result might be different for you :)
Random Number Generator Example

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:

np.random.randint(10,50) gives output as 45.
Generate a Random Integer in the range [10,50)
Result of np.random.random(). It generates a random float in the range [0.0,0.1)
Generate a Random Float in the range [0.0,0.1)

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.seed(10); np.random.rand(); 0.771320643266746 is always the first random number for seed value=10.
Seed in Python

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.

Math.floor(Math.random() * (max — min) ) + min; JavaScript equivalent of Python randint().
Generate a random integer between min and max (JavaScript)
Snapshot of Random Number Generator on CodePen
Generates a random number between 105 and 1000

min = 105; max = 1000; => Math.floor(Math.random()*895)+105

A Snapshot of Random Number Generator Built by Me on Codepen.
A Snapshot of The Random Number Generator Built by Me

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!

--

--

Athisha R K
Analytics Vidhya

Infrastructure Engineer @ Lowe’s India | Python Programmer