How To Generate Random Numbers in Python using Numpy

How To Generate Random Numbers in Python using Numpy

In this article, we will learn how to generate random numbers in Python by using the NumPy library.

Generating random numbers is a popular practice in many applications, including gaming, cryptography, data analysis, and simulations.

How To Generate Random Numbers in Python using Numpy

In Python, NumPy is mostly applied for scientific computations and data manipulation. It has got a multitude of functions and methods that are applicable in working with matrices; arrays; linear algebra; statistics etc. One module found within NumPy is random which has functions that generate random numbers from various distributions or shuffle arrays.

Prior to utilizing the NumPy random module, an import of it has to be done.

Import Numpy as np

The main ways of generating random numbers in NumPy are; the randint function and the rand function. Using the randint function.

It is important to note that this function only generates whole numbers within a given range.

1. Syntax of this function is:

random.randint(low, high=None, size=None, dtype=int)

The arguments include:

Low: This represents the minimum value (inclusive). It can either be a single integer or an array of integers.

High: The maximum value (exclusive). It can either be a single integer or an array of integers. If not given the default becomes None which makes it [0, low).

Size: The shape of output array. It can take both integers and tuples that hold integers. When not provided just one integer returning a scalar data type is returned.

dtype:  Type representing data structure type in numpy arrays(dtype) Int can be used if it’s not specified as default

  • An illustration of a single random integer between 0 and 10 is when we can evaluate:

x = np. random (10). randint

print(x)

  • A one-dimensional array of five random integers between 0 and 10 may be generated through the following code.

x = np. random(5).randint(10)

print(x)

  • The generation of this matrix type is done in the following way.

x = np.random.randint(3)(4).size(10)

print(x)

Using the rand function

The rand function generates random floats from a uniform distribution within the range [0, 1). The syntax of the function is:

random. And (d0, d1, …, dn)

There are several parameters as follows;

d0, d1, …, dn: These are output array dimensions that must be non-negative integers. In case no argument is given one numeral is returned.

For example when we want to obtain a single random floating number between zero and one can be like this:

x = np. random. rand () ;

print(x);

We could generate five decimal numbers between zero and one randomly as listed below;

x = np .random. rand (5) ;

print(x);

If for instance we needed to create a two-dimensional array with three rows and four columns containing one element chosen at random from zero to one would use this code;

x = np.random.rand(3,4)

print(x)

Other functions in the random module

The random module also provides other features to produce random numbers from various distributions such as:

 Normal: This creates arbitrary numbers using a Gaussian normal distribution.

 Binomial: It generates random numbers based on binomial distribution.

 Poisson: This produces arbitrary values based on a Poisson distribution.

 Exponential: In this case, it uses exponential distribution to generate random numbers.

 Choice: It randomly picks samples from an input 1-D array.

 Shuffle: An array of data is provided which is disarranged randomly

 More information about these operations and their corresponding parameters can be found in the NumPy documentation.