NumPy random.uniform() function in Python is used to create arrays filled with random samples which are 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 uniform distribution of random samples of the single and multi-dimensional arrays with examples. 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 Python NumPy random.uniform() Function
Following are quick examples of random.uniform() Function. For more examples of NumPy array refer to NumPy Tutorial.
# Below are the quick examples # 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. 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. 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 we do not provide it, it will return a single random sample between
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 we do not provide any parameter to this function it will return a single random sample of uniform distribution. We will get a different random sample when we run the same code multiple times.
import numpy as np # Get the single uniform random sample arr = np.random.uniform() print(arr) # Output : # 0.34063534472196755
4. Get the Uniform Random Samples of 1-D Array
When we 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. Let’s take an example,
# Get the uniform samples of an array arr = np.random.uniform(size = 5) print(arr) # Output: # [0.73820649 0.98286584 0.05413008 0.3165721 0.95928769]
5. Get the Random Samples of Given Interval
high parameters into this function along with the specified size, it will return the array of random samples between the
high values. For example,
# Get the uniform random samples arr = np.random.uniform(low = 3, high = 5, size = 5) print(arr) # Output : # [3.87875762 4.20867489 4.13948288 3.28137881 4.4622086 ]
6. Get the Random Samples of Multi-Dimensional Array
When we 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. Let’s take an example,
Using uniform() method # Get the random sample of multi-Dimensional array arr = np.random.uniform(low = 3, high = 5, size = (2, 3)) print(arr) # Output : # [[3.18967671 3.93263376 4.31706326] # [3.85327017 4.22567335 4.91062538]]
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()
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 value.
- Python NumPy array copy
- Python NumPy Concatenate() Function
- How to create an array using ones() function?
- Get the shape of an array
- 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?