python-scipyHow can I use Python and SciPy to generate a uniform distribution?
To generate a uniform distribution using Python and SciPy, you can use the scipy.stats.uniform
module. This module provides a uniform
class that takes two parameters: loc
and scale
. loc
is the lower bound of the distribution, and scale
is the difference between the upper and lower bounds.
For example, the following code will generate a uniform distribution between 0 and 10:
import scipy.stats as stats
uniform_dist = stats.uniform(loc=0, scale=10)
You can then generate random numbers from this distribution using the rvs
function:
random_numbers = uniform_dist.rvs(size=10)
print(random_numbers)
# [9.89569399 7.01456662 8.98391771 8.91355863 3.97722255 5.07906918
# 7.68154078 6.25356775 0.66108565 5.56420246]
Code explanation
scipy.stats.uniform
: module containing theuniform
classuniform
: class used to generate a uniform distributionloc
: lower bound of the distributionscale
: difference between the upper and lower boundsrvs
: function used to generate random numbers from the distribution
Helpful links
More of Python Scipy
- How can I check if a certain version of Python is compatible with SciPy?
- How do I use Python Numpy to read and write Excel (.xlsx) files?
- How do I create a 2D array of zeros using Python and NumPy?
- How do I use Python Scipy to perform a Z test?
- How do I check the version of Python Scipy I am using?
- How do I uninstall Python Scipy?
- How do I update Python SciPy?
- How do I use the NumPy transpose function in Python?
- How do I use the scipy ttest_ind function in Python?
- How do I upgrade my Python Scipy package?
See more codes...