The NumPy random.uniform()
function in Python is used to generate arrays filled with random samples from a uniform distribution. Uniform distribution is a probability-related distribution. This function returns the samples that are uniformly distributed over the given intervals of low and high. In uniform samples result, it includes low but excludes high.
In this article, I will explain np.random.uniform()
function syntax and using its parameters, how to get a uniform distribution of random samples of the single and multi-dimensional arrays. To create an array of uniform random values, pass size
as int or tuple of int as an argument to this function, similar to other NumPy functions like the ones(), zeros(), and random.normal().
1. Quick Examples of NumPy random.uniform() Function
If you are in a hurry, below are some quick examples of how to use NumPy random.uniform() in Python.
# Quick examples of numpy random.uniform() function
# Example 1: Get the single uniform random sample
arr = np.random.uniform()
# Example 2: Get the uniform samples of an array
arr = np.random.uniform(size = 5)
# Example 3: Get the random samples between low and high
arr = np.random.uniform(low = 3, high = 5, size = 5)
# Example 4: Get the random sample of multi-Dimensional array
arr = np.random.uniform(low = 3, high = 5, size = (2, 3))
# Example 5: Plot the result
gfg = np.random.uniform(-2, 8, 1000)
plt.hist(gfg, bins = 50, density = True)
plt.show()
2. Syntax of NumPy random.uniform()
Following is the syntax of random.uniform().
# Syntax of NumPy random.uniform()
numpy.random.uniform(low=0.0, high=1.0, size=None)
2.1 Parameters of random.uniform()
Following are the Parameters of random.uniform()
low
: It is a lower boundary of the output interval (inclusive). Samples, which are generated by uniform distribution are greater than or equal to low. The default low value is 0.high
: It is a higher boundary of the output interval (exclusive). Samples, which are generated by uniform distribution are less than or equal to high. The default value is 1.0.size
: It specifies the size of the output array. If it is set to a single integer the output array is a 1-D array and set to a tuple of integers the output array is a multi-dimensional array. If you do not provide it, it will return a single random sample betweenlow
andhigh
.
2.2 Return Value of random.uniform()
It returns an array of random samples that are taken from the uniform distribution over the given intervals. If the size is not provided, it will return a single random sample.
3. Usage of random.uniform()
Numpy is a package for working with numeric data in Python. As we know NumPy has many functions that are used mostly for creating an arrays and some of them are used for manipulating the data in an array. random.uniform()
function is the built-in function, it generates random samples from a uniform distribution and creates a NumPy array with these values.
If you do not provide any parameter to this function it will return a single random sample of uniform distribution. You will get a different random sample when you run the same code multiple times.
In the below example, you import the NumPy module with the alias np
. Then, you use np.random.uniform()
to generate a single random float from the default interval [0.0, 1.0). Finally, you print the result.
# Import numpy module
import numpy as np
# Get the single uniform random sample
arr = np.random.uniform()
print("Random float:\n",arr)
Yields below output.
4. Get the Uniform Random Samples of 1-D Array
When You pass the int
as a value to the size
parameter to this function, it will return the array of the random samples with the length specified with size
.
To generate a 1-D array of uniform random samples. It uses NumPy’s random.uniform()
function. This program generates a 1-D array (arr
) containing 5 uniform random samples from the default interval [0.0, 1.0). If you want to specify a different interval, you can use the low
and high
parameters in the np.random.uniform()
function.
# Import numpy module
import numpy as np
# Get the uniform samples of an array
arr = np.random.uniform(size = 5)
print("1-D array of uniform random samples:\n",arr)
Yields below output.
5. Get the Random Samples of Given Interval
Specify the low
and high
parameters into this function along with the specified size, it will return the array of random samples between the low
and high
values.
To generate a 1-D array of uniform random samples within the specified interval [3, 5). For instance, you import the NumPy module using import numpy as np. You use np.random.uniform(low=3, high=5, size=5)
to generate a 1-D array with 5 uniform random samples from the interval [3, 5). The result is stored in the variable arr
.
# Get the uniform random samples
arr = np.random.uniform(low = 3, high = 5, size = 5)
print("1-D array of uniform random samples:\n",arr)
# Output:
# 1-D array of uniform random samples:
# [3.38575732 3.00144016 3.76668713 3.12292346 4.26992286]
6. Get the Random Samples of Multi-Dimensional Array
When you pass the tuple of int as a parameter into this function along with specified intervals, it can return a multi-dimensional NumPy array of random samples which are formed from the uniform distribution. The shape of the NumPy array would be equal to the value of the size param.
To generate a multi-dimensional array of random samples within the specified interval [3, 5). You use np.random.uniform(low=3, high=5, size=(2, 3))
to generate a 2×3 multi-dimensional array with random samples from the interval [3, 5). The result is stored in the variable arr
.
# Using uniform() method
# Get the random sample of multi-Dimensional array
arr = np.random.uniform(low = 3, high = 5, size = (2, 3))
print("Multi-dimensional array of random samples:\n",arr)
# Output:
# Multi-dimensional array of random samples:
# [[4.70439459 3.82377021 4.12680258]
# [3.11214053 4.12974219 4.18795926]]
7. Graphical Presentation of random.uniform()
To make a histogram with the help of the pyplot
library and print the graph of the NumPy random uniform distribution.
import matplotlib.pyplot as plt
# Using uniform() method
gfg = np.random.uniform(-2, 8, 1000)
plt.hist(gfg, bins = 50, density = True)
plt.show()
Frequently Asked Questions
The NumPy random uniform function, numpy.random.uniform()
, is used to generate random samples from a uniform distribution. This distribution ensures that all values within a specified interval are equally likely to be drawn.
The interval is specified by the low
and high
parameters. The generated random samples are drawn from the interval [low, high), meaning that the lower bound is inclusive, and the upper bound is exclusive.
You can generate random samples for both one-dimensional and multi-dimensional arrays. For a 1-D array, you can specify the size parameter as an integer. For a multi-dimensional array, you can use a tuple to define the shape of the array.
You can generate a single random float by calling the function without specifying the size
parameter.
You can generate random samples within a specified interval for a multi-dimensional array using the numpy.random.uniform()
function. You need to provide the low
, high
, and size
parameters accordingly.
The lower bound (low
) is inclusive, while the upper bound (high
) is exclusive. This means that values equal to the lower bound may be generated, but values equal to the upper bound will not be generated.
Conclusion
In this article, I have explained NumPy random.uniform()
and using this how to get the random values of 1-D NumPy array and multidimensional array, where the elements are from a uniform distribution between low and high values.
Related Articles
- Python NumPy array copy
- Python NumPy Concatenate() Function
- How to create an array using ones() function?
- Get the shape of an array
- How to Compute Standard Deviation in NumPy
- NumPy Count Nonzero Values in Python
- How to Use NumPy Argsort() in Python
- How to create an array using the zeros() function?
- How to Use NumPy random.randint() in Python
- How to Use Numpy random.rand() in Python
- How to Use NumPy random.randn() in Python?
- How to Use NumPy Random choice() in Python?