Skip to content
scipython.com

Q6.6.1Using np.random.randint

Explain the difference between

In [x]: a = np.array([6,6,6,7,7,7,7,7,7])
In [x]: a[np.random.randint(len(a), size=5)]
array([7, 7, 7, 6, 7])      # (for example)

and

In [x]: np.random.randint(6, 8, 5)
array([6, 6, 7, 7, 7])      # (for example)
Show solution

The first case,

In [x]: a = np.array([6,6,6,7,7,7,7,7,7])
In [x]: a[np.random.randint(len(a), size=5)]
array([7, 7, 7, 6, 7])      # (for example)

Samples randomly from the array a with replacement: for each item selected the probability of a 6 is \(\frac{1}{3}\) and the probability of a 7 is \(\frac{2}{3}\).

In the second case,

In [x]: np.random.randint(6, 8, 5)
array([6, 6, 7, 7, 7])      # (for example)

the numbers are drawn from \([6,7]\) uniformly, so the probabilities of each number being selected is \(\frac{1}{2}\).