Generating Uniformly Distributed Random Numbers
The core function for generating random numbers from a uniform distribution in NumPy is numpy.random.uniform()
. This function offers flexibility in controlling the range and the number of random samples generated. Let’s explore its usage with several examples.
Generating a single random number:
The simplest use case involves generating a single random float between 0 (inclusive) and 1 (exclusive):
import numpy as np
= np.random.uniform()
single_random_number print(single_random_number)
Specifying the range:
You can control the range of the generated numbers using the low
and high
parameters. For instance, to generate a random number between 5 and 15:
= np.random.uniform(low=5, high=15)
random_number_in_range print(random_number_in_range)
Generating an array of random numbers:
To generate an array of random numbers, specify the desired size
as a tuple:
= np.random.uniform(low=0, high=10, size=(3, 4)) # 3x4 array
array_of_random_numbers print(array_of_random_numbers)
This creates a 3x4 array filled with random numbers between 0 and 10.
Controlling the data type:
By default, np.random.uniform()
returns floating-point numbers. However, you can specify the dtype
parameter to change the data type:
= np.random.uniform(low=1, high=10, size=5, dtype=int)
random_integers print(random_integers) # Note: this will truncate the floating point numbers to integers.
This example demonstrates how to generate an array of integers. Remember that the dtype=int
truncates any decimal part generated by uniform
. For more fine-grained integer control, consider other functions like numpy.random.randint
.
Understanding the Uniform Distribution’s Properties
The uniform distribution’s simplicity is deceptive. Its key properties are:
- Equal Probability: Every point within the specified range has an equal probability of being selected.
- Defined Range: The distribution is bounded by a minimum (
low
) and a maximum (high
) value. - Continuous: The distribution is continuous, meaning any value within the range is possible (though the precision is limited by the floating-point representation).
These properties make the uniform distribution a building block for more complex distributions and simulations. For example, it’s often used to generate random points within a specific geometric shape or to initialize weights in neural networks. We’ll explore more advanced use cases in future posts.
Beyond the Basics: Seeding for Reproducibility
For reproducible results, it’s essential to set a seed using np.random.seed()
:
42) # Set the seed for reproducibility
np.random.seed(= np.random.uniform(size=5)
random_numbers print(random_numbers)
42) # Same seed, same results
np.random.seed(= np.random.uniform(size=5)
random_numbers_again print(random_numbers_again)
Setting the seed ensures that the sequence of random numbers generated is consistent across different runs of your code. This is invaluable for debugging and sharing your work. Remember to set the seed before calling np.random.uniform()
.