Generate Random Numbers From The Uniform Distribution using NumPy


Commonly used in statistical analysis and data modelling, the Gaussian distribution, alternatively referred to as the normal distribution, presents a familiar bell-shaped curve that represents a continuous probability distribution for a real-valued random variable. Its bell-shaped curve characterises it and is often used to model real-world phenomena.

The random module generates a set of pseudorandom numbers drawn from a normal distribution with a stipulated mean and standard deviation.

Syntax

numpy.random.uniform(low=0.0, high=1.0, size=None)

Parameters

low : It takes floating or array like values as its argument represents the minimum value that can be generated. The default value is set as 0.

high: Very similar to low but the default value is set as 1.

size : It takes integer or tuple of integer values as its argument and determines the number of random numbers to be generated.

Example

The following example shows how to generate random numbers with default lower and upper bounds (0 and 1).

Algorithm

  • Import the numpy library as nmp.

  • Use the numpy.random.uniform function() with no explicit lower and upper bounds specified.

  • Specify the size parameter as 5 to generate five random numbers from the uniform distribution.

  • Store the generated random numbers in a separate variable.

import numpy as nmp

# Generate five random numbers from the uniform distribution
random_numbers = nmp.random.uniform(size=5)

# Print the generated random numbers
print("The Random Generated Numbers Are:", random_numbers)

Output

The Random Generated Numbers Are: [0.16686196 0.00839822 0.70146929 0.98215339 0.65254042]

Example

The following code illustrates generating 10 random numbers with a specific range from the uniform distribution using numpy.

import numpy as np

# Set the lower and upper bounds
lower_bound = 10  # Lower bound of the distribution
upper_bound = 20  # Upper bound of the distribution

# Generate five random numbers from the uniform distribution
random_numbers = np.random.uniform(low=lower_bound, high=upper_bound, size=10)

# Print the generated random numbers
print("Generated Random Numbers:", random_numbers)

Output

Generated Random Numbers: [12.17244749 19.45438526 17.23474221 12.62434952 
14.93329819 19.24373114 14.46020672 10.5430179  13.23576976 16.82255347]

Example

In this example, we will generate a 2D array of random numbers from a custom range using NumPy's Uniform Distribution.

Algorithm

  • Import the numpy library.

  • Set the desired lower bound and upper bound values for the uniform distribution.

  • Pass the lower bound and upper bound values as arguments and specify the size of the parameters.

  • Store the generated random numbers in a separate variable.

import numpy as nmpy

# Set the lower and upper bounds
lower_bound = 0 
upper_bound = 100 

# Generate a 2D array of random numbers from the uniform distribution
random_numbers = nmpy.random.uniform(low=lower_bound, high=upper_bound, size=
(3, 5))

# Print the generated random numbers
print("The 2D array Randomly generated Numbers:")
print(random_numbers)

Output

The 2D array Randomly generated Numbers:
[[34.9530181  47.99901914 80.16861203  5.86164601 24.51089145]
 [87.87714454 79.73164792 17.19521485 67.10860954 54.12845578]
 [75.46746683 55.00061495 93.79876457 74.6587852  33.00568042]]

Conclusion

NumPy is a powerful tool for developers to generate random numbers from the uniform distribution in Python that can be used in various applications, simulations and generating random data points for test cases which is frequently used to generate synthetic data for ML applications to create random inputs.

Updated on: 10-Aug-2023

428 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements