# NumPy Random | Python Numpy tutorials 15 | Better4Code

**NumPy Random**: It is a well-known Python package for scientific computing that includes a number of utilities for generating random numbers. Several scientific and technical applications, including as simulations, statistical analysis, and machine learning, rely heavily on random number generation. In this post, we’ll look at the NumPy random module and its many random number generation routines.

**The NumPy Random Module**

The NumPy random module contains a set of functions for generating random integers from various probability distributions. The module is simple to use and offers a large range of methods for generating random numbers of various sorts and distributions.

**Generating Random Numbers**

NumPy provides several functions for generating random numbers of different types and distributions. Here are some of the most commonly used functions:

**1. rand()**

The rand() function creates random numbers between 0 and 1 from a uniform distribution. The function only accepts one input, the form of the array of random integers to be created. Here’s an illustration:

import numpy as np # Generate a 2x3 array of random numbers between 0 and 1 a = np.random.rand(2, 3) print(a)

The output of this code will be a 2×3 array of random numbers between 0 and 1.

**2. randint()**

The randint() method creates numbers at random between two provided values. The function accepts two arguments: the lowest and maximum values for the integer range to be created, as well as an optional parameter specifying the structure of the output array. Here’s an illustration:

# Generate a 1x5 array of random integers between 1 and 10 b = np.random.randint(1, 10, size=5) print(b)

The output of this code will be a 1×5 array of random integers between 1 and 10.

**3. normal()**

The normal() function generates random numbers from a normal distribution with a specified mean and standard deviation. The function takes two arguments, the mean and standard deviation of the distribution, and an optional argument for the shape of the output array. Here’s an example:

# Generate a 3x3 array of random numbers from a normal distribution with mean 0 and standard deviation 1 c = np.random.normal(0, 1, size=(3, 3)) print(c)

The output of this code will be a 3×3 array of random numbers from a normal distribution with mean **0** and **standard **deviation 1.

**4. uniform()**

The uniform() function generates random numbers from a uniform distribution between two specified values. The function takes two arguments, the minimum and maximum values for the range of numbers to be generated, and an optional argument for the shape of the output array. Here’s an example:

# Generate a 2x2 array of random numbers between 0 and 5 d = np.random.uniform(0, 5, size=(2, 2)) print(d)

The output of this code will be a 2×2 array of random numbers between 0 and 5.

**5. choice()**

The choice() function generates random samples from a given array. The function takes two arguments, the array to sample from and the number of samples to generate. Here’s an example:

# Generate a random sample of 3 elements from the array [1, 2, 3, 4, 5] e = np.random.choice([1, 2, 3, 4, 5], size=3) print(e)

The output of this code will be a random sample of 3 elements from the array [1, 2, 3, 4,5].

### Seeding the Random Number Generator

In scientific computing, it’s often important to generate reproducible results. One way to achieve this is by setting a seed for the random number generator. The seed determines the starting point for the sequence of random numbers generated by the random number generator.

By setting the seed to a specific value, you can ensure that the sequence of random numbers generated by the random number generator is always the same.

NumPy provides a seed() function for setting the seed of the random number generator. Here’s an example:

import numpy as np # Set the seed for the random number generator np.random.seed(123) # Generate a 2x2 array of random numbers between 0 and 1 a = np.random.rand(2, 2) print(a)

The output of this code will be a 2×2 array of random numbers between 0 and 1, but the sequence of random numbers generated will always be the same, as long as the seed remains the same.

**Conclusion**

In this article, we’ve explored the NumPy random module and its various functions for generating random numbers. We’ve looked at some of the most commonly used functions for generating random numbers of different types and distributions, and we’ve seen how to set the seed of the random number generator to generate reproducible results.

NumPy provides a powerful and flexible set of tools for generating random numbers that are widely used in scientific computing and engineering applications. Whether you need to generate random numbers for simulations, statistical analysis, or machine learning, NumPy has you covered.

With its easy-to-use syntax and wide range of functions, NumPy is a must-have tool for any data scientist or engineer working with Python.