Random numbers

NumPy has functions for creating arrays of random numbers from different distributions in np.random, as well as handling things like permutation, shuffling, and choosing.

Here is the numpy.random documentation.


In [ ]:
# We use matplotlib for a moment to make some quick plots
import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt
%matplotlib inline

In [ ]:
import numpy as np

In [ ]:
plt.hist(np.random.random(10000))
plt.title('Uniform Random Distribution $[0,1]$')
plt.xlabel('value')
plt.ylabel('count')

In [ ]:
plt.hist(np.random.randn(1000))
plt.title('Standard Normal Distribution')
plt.xlabel('value')
plt.ylabel('count')

The shuffle function shuffles an array in place:


In [ ]:
a = np.arange(0,10)
print(a)
np.random.shuffle(a)
print(a)

The permutation function does the same thing but first makes a copy:


In [ ]:
a = np.arange(0,10)
print(np.random.permutation(a))
print(a)

The choice function provides a powerful way of creating synthetic data sets of discrete data:


In [ ]:
np.random.choice(['m','f'], 20, p=[0.25,0.75])

In [ ]: