9951 explained code solutions for 126 technologies


python-scipyHow can I use Python, SciPy and stats.rvs to generate random variables?


Python, SciPy and stats.rvs can be used together to generate random variables. To do this, the following steps can be taken:

  1. Import the required libraries.

    import numpy as np
    import scipy.stats as stats
  2. Generate random variables using stats.rvs.

    
    # Generate a random variable with a normal distribution
    normal_dist = stats.rvs(size=1000, loc=0, scale=1)

Generate a random variable with a uniform distribution

uniform_dist = stats.rvs(size=1000, loc=0, scale=10)


3. Use the generated variables to plot a histogram.
```python
import matplotlib.pyplot as plt

# Plot the normal distribution
plt.hist(normal_dist, bins=20, color='c')
plt.axvline(normal_dist.mean(), color='b', linestyle='solid', linewidth=2)
plt.axvline(normal_dist.mean() + normal_dist.std(), color='b', linestyle='dashed', linewidth=2)
plt.axvline(normal_dist.mean() - normal_dist.std(), color='b', linestyle='dashed', linewidth=2)
plt.show()

# Plot the uniform distribution
plt.hist(uniform_dist, bins=20, color='c')
plt.axvline(uniform_dist.mean(), color='b', linestyle='solid', linewidth=2)
plt.axvline(uniform_dist.mean() + uniform_dist.std(), color='b', linestyle='dashed', linewidth=2)
plt.axvline(uniform_dist.mean() - uniform_dist.std(), color='b', linestyle='dashed', linewidth=2)
plt.show()

Helpful links

Edit this code on GitHub