random.uniform()
: R runif()
Equivalent Function in Python
In R, the runif()
function generates random numbers from a uniform distribution.
In Python, numpy.random.uniform()
(from NumPy package) function is equivalent to runif()
fuction from R for generating random numbers
from a uniform distribution.
In a uniform distribution, every value within the given range is equally likely to occur. The uniform distribution is mostly used in the generation of random numbers.
The general syntax of numpy.random.uniform()
looks like this:
# import package
import numpy as np
# generate random numbers
np.random.uniform(low = 0, high = 1, size = 10)
Where,
Parameter | Description |
---|---|
low |
minimum value for the random numbers (default is 0) |
high |
maximum value for the random numbers (default is 1) |
size |
number of random numbers to generate |
The following examples explains how to use the numpy.random.uniform()
function to generate random numbers in Python.
Example 1: Generate random numbers between 0 and 1
The following example shows how to generate 5 random numbers between 0 and 1 using runif()
equivalent in Python.
By default, the numpy.random.uniform()
function generates random numbers between 0 to 1 from a uniform distribution.
If you run the
numpy.random.uniform()
function multiple times, you may see different results every time. To get reproducible random results every time, you should usenumpy.random.seed()
function.
# import package
import numpy as np
# set random seed for reproducible results
np.random.seed(2)
# generate 5 random values between 0 to 1
np.random.uniform(size = 5)
# output
array([0.4359949 , 0.02592623, 0.54966248, 0.43532239, 0.4203678 ])
You can see that the 5 random values between 0 and 1 are generated from the uniform distribution.
Example 2: Generate random numbers between 1 and 50
The following example shows how to generate 10 random numbers between 1 and 50 from uniform distribution using
numpy.random.uniform()
function.
# import package
import numpy as np
# set random seed for reproducible results
np.random.seed(2)
# generate 10 random values between 1 to 50
np.random.uniform(low = 1, high = 50, size = 10)
# output
array([22.3637502 , 2.27038536, 27.93346142, 22.33079724, 21.5980223 ,
17.18640623, 11.02778307, 31.34427735, 15.68307901, 14.07453648])
You can see that the 10 random values between 1 and 50 are generated from the uniform distribution.
Example 3: Generate histogram from uniform distribution
You can also numpy.random.uniform()
function to generate random numbers and plot a histogram of uniform distribution (rectangular
distribution).
The following example generates 1000 random numbers and plots a histogram,
# import package
import numpy as np
import matplotlib.pyplot as plt
# generate 1000 random integer values between 1 to 10
ran_num = np.random.uniform(low = 1, high = 10, size = 1000)
# Create a histogram to visualize uniform distribution
sns.histplot(data = ran_num)
plt.show()
The above histogram visualizes the distribution of 1000 random numbers generated using the numpy.random.uniform()
function.
Related: How to Use runif in R (Practical Examples)
Enhance your skills with courses on Statistics and Python
- Introduction to Statistics
- Python for Everybody Specialization
- Python 3 Programming Specialization
- Statistics with Python Specialization
- Advanced Statistics for Data Science Specialization
This work is licensed under a Creative Commons Attribution 4.0 International License
Some of the links on this page may be affiliate links, which means we may get an affiliate commission on a valid purchase. The retailer will pay the commission at no additional cost to you.