How to generate random number in Python?

How to generate random number in Python?
How to generate random number in Python?

The use of randomness is an important part of the configuration and evaluation in Python and its application. In this article, we will discover how to generate and work with random numbers in Python.

Especially machine learning algorithms (Python is a key element) from the random initialisation of weights in an artificial neural network to the splitting of data into random train and test sets, to the random shuffling of a training dataset in stochastic gradient descent. Generating random numbers and harnessing randomness is a required skill.

After completing this article, we will know how randomness can be applied in programs via the use of pseudorandom number generators. Also by using which Python standard library we are able to use randomness and generate random numbers. Again we try to explore more ways to generate arrays of random numbers via the NumPy library. So, basically, there are three different ways to generate any random number into our program. Let’s discuss them one by one.

Pseudorandom Number Generators:

The idea of randomness that we put into our programs and algorithms is a mathematical trick called a pseudorandom number generator. A random number generator is a system that generates random numbers from a true source of randomness. Unlike something physical, such as a Geiger counter, where the results are turned into random numbers. In machine learning, we do not need true randomness instead we can use pseudorandomness.

Pseudorandomness is a sample of numbers that look close to random but were generated using a deterministic process. Shuffling data and initialising coefficients with random values use pseudorandom number generators. These little programs are often a function that if we call in return they will produce a random number, again called, they will return a new random number. Wrapper functions are often also available and allow us to get our randomness as an integer, floating-point, within a specific distribution, within a specific range, and so on.

The numbers are generated in a sequence. The sequence is deterministic and is seeded with an initial number. If we do not explicitly seed the pseudorandom number generator, then it may use the current system time in seconds or milliseconds as the seed. The value of the seed does not matter. Choose whatever we want from the program. What does matter is that the same seeding of the process will result in the same sequence of random numbers.

Generating Random Number with a standard library of Python:

The Python standard library provides a module called “random” that offers a suite of functions for generating random numbers. Python uses a popular and robust pseudorandom number generator called the Mersenne Twister. In this section, we will look at a number of use cases for generating and using random numbers and randomness with the standard Python API.

Seed The Random Number Generator:

The pseudorandom number generator is a mathematical function that generates a sequence of nearly random numbers. It takes a parameter to start off the sequence, called the seed. The function is deterministic, meaning given the same seed, it will produce the same sequence of numbers every time. The choice of seed does not matter.

The seed() function will seed the pseudorandom number generator, taking an integer value as an argument, such as 1 or 7. The example below demonstrates seeding the pseudorandom number generator, generates some random numbers, and shows that reseeding the generator will result in the same sequence of numbers being generated.

Running the above example seeds the pseudorandom number generator with the value 1, generates 3 random numbers, reseeds the generator, and shows that the same three random numbers are generated.

10.13436424411240122 0.8474337369372327 0.763774618976614
20.13436424411240122 0.8474337369372327 0.763774618976614

It can be useful to control the randomness by setting the seed to ensure that our code produces the same result each time, such as in a production model. For running experiments where randomisation is used to control for confounding variables, a different seed may be used for each experimental run.

blog banner 1

Generating Random Integer Number:

Random integer values can be generated with the randint() function.This function takes two arguments: the start and the end of the range for the generated integer values. Random integers are generated within and including the start and end of range values, specifically in the interval [start, end]. Random values are drawn from a uniform distribution.

The example below generates 10 random integer values between 0 and 10.

After running the above example, we got the results as it generates and prints 10 random integer number.


Generating Random Numbers using NumPy:

In Python’s some application area like machine learning, we are likely using libraries such as sci-kit learn and Keras. These libraries make use of NumPy under the covers, a library that makes working with vectors and matrices of numbers very efficient. NumPy also has its own implementation of a pseudorandom number generator and convenience wrapper functions. NumPy also implements the Mersenne Twister pseudorandom number generator.
Let’s take a look at a few examples of generating random numbers and using randomness with NumPy arrays.

Seed The Random Number Generator:

The NumPy pseudorandom number generator is different from the Python standard library pseudorandom number generator. More Importantly, seeding the Python pseudorandom number generator does not impact the NumPy pseudorandom number generator. It must be seeded and used separately. The seed() function can be used to seed the NumPy pseudorandom number generator, taking an integer as the seed value.
The example below demonstrates how to seed the generator and how reseeding the generator will result in the same sequence of random numbers being generated.

Running the example seeds the pseudorandom number generator, prints a sequence of random numbers, then reseeds the generator showing that the exact same sequence of random numbers is generated.

[4.17022005e-01 7.20324493e-01 1.14374817e-04]
[4.17022005e-01 7.20324493e-01 1.14374817e-04]

Generating Array of Random Integer Number:

An array of random integers can be generated using the randint() NumPy function. This function takes three arguments, the lower end of the range, the upper end of the range, and the number of integer values to generate or the size of the array. Random integers will be drawn from a uniform distribution including the lower value and excluding the upper value, e.g. in the interval [lower, upper).

The example below demonstrates generating an array of random integers.

Running the example generates and prints an array of 20 random integer values between 0 and 10.
[5 8 9 5 0 0 1 7 6 9 2 4 5 2 4 2 4 7 7 9]

How to Randomly Shuffle a List:

Randomness can be used to shuffle a list of items, like shuffling a deck of cards. The shuffle() function can be used to shuffle a list. The shuffle is performed in place, meaning that the list provided as an argument to the shuffle() function is shuffled rather than a shuffled copy of the list being made and returned.
The example below demonstrates randomly shuffling a list of integer values.

When we run the above example it first prints the list of integers, then printing the same list after it has been randomly shuffled like:

1. [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19]
2. [11, 5, 17, 19, 9, 0, 16, 1, 15, 6, 10, 13, 14, 12, 7, 3, 8, 2, 18, 4]


In this article, we discovered a lot of things about how to generate and work with random numbers in Python as it has a large application in the new era advanced technology like Machine Learning more Specifically, we observe from this article that

  • The randomness can be applied in programs via the use of a pseudorandom number generator.
  • How to generate random numbers and use randomness via the Python standard library.
  • How to generate arrays of random numbers via the NumPy library.

So, Random number does NOT mean a different number every time. Random means something that can not be predicted logically.

Check out Modules & Packages in Python.