Lesson 04
Random Arrays
Generate random arrays with randint, rand, uniform, randn, normal, and choice, then reorder values with shuffle and permutation.
np.random builds arrays of random values for tests, simulations, and sample data. Pass size as a number or a shape tuple. Set a seed with np.random.seed(n) when you want the same values on every run of the same machine.
randint
randint(high) picks one whole number from 0 up to high - 1. randint(low, high, size) fills a shape. The high value is never picked, so a six-sided die is randint(1, 7).
import numpy as np
np.random.seed(7)
rolls = np.random.randint(1, 7, size=10)
print(rolls)
print(rolls.min() >= 1, rolls.max() <= 6)
print(np.random.randint(1, 100, size=(2, 3)).shape)Output from one run (random values change between runs and machines):
[5 2 4 4 5 2 1 2 3 3]
True True
(2, 3)rand and uniform
rand gives floats from 0 up to, but not including, 1. Pass the dimensions as separate numbers. uniform(low, high, size) gives floats in your own range.
import numpy as np
np.random.seed(7)
print(np.round(np.random.rand(3), 3))
print(np.random.rand(2, 4).shape)
temps = np.random.uniform(18, 26, size=4)
print(np.round(temps, 1))Output from one run (random values change between runs and machines):
[0.076 0.78 0.438]
(2, 4)
[24.4 21. 18.5 20.3]randn and normal
randn draws from the standard normal distribution: mean 0 and standard deviation 1. normal(loc, scale, size) sets the mean with loc and the spread with scale. With many samples, the measured mean and spread land close to what you asked for.
import numpy as np
np.random.seed(7)
print(np.round(np.random.randn(4), 3))
heights = np.random.normal(loc=170, scale=8, size=10000)
print(round(heights.mean()), round(heights.std()))Output from one run (random values change between runs and machines):
[ 1.691 -0.466 0.033 0.408]
170 8choice, shuffle, and permutation
choice(values, size) picks items, with repeats allowed. shuffle reorders an array in place and returns None. On a 2-D array it moves whole rows. permutation returns a reordered copy and leaves the original unchanged.
import numpy as np
np.random.seed(7)
print(np.random.choice(["red", "green", "blue"], size=5))
cards = np.arange(1, 6)
np.random.shuffle(cards)
print(cards)
original = np.arange(1, 6)
mixed = np.random.permutation(original)
print(mixed, original)Output from one run (random values change between runs and machines):
['red' 'green' 'blue' 'red' 'green']
[4 5 3 2 1]
[2 4 5 1 3] [1 2 3 4 5]Newer code often creates a generator with rng = np.random.default_rng(7) and calls rng.integers(1, 7, size=5) or rng.normal(170, 8, size=5). The ideas are the same.
Key takeaways
-
randint(low, high)never returnshigh -
randanduniformgive floats.randnandnormalgive a bell curve -
shufflechanges the array and returnsNone -
permutationreturns a new order and keeps the original -
Next: Indexing
-
Previous: Creating Arrays
Loading playground…
Check your understanding
Random Arrays Quiz
Which values can np.random.randint(1, 7) return?
1 / 10