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.
10.13436424411240122 0.8474337369372327 0.763774618976614
20.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.
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.
2
9
1
4
1
3
7
7
7
10
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]
Conclusion:
In this article, we explored how to generate and work with random numbers in Python, a concept widely used in modern technologies like Machine Learning. We learned that randomness in Python is implemented using pseudorandom number generators, how to create random numbers through the standard library, and how to generate arrays of random numbers with NumPy. Importantly, random does not mean producing a new number each time, but rather generating values that cannot be logically predicted.
Recommended Readings: