# 4.5. Array Random

• Since numpy v1.17: BitGenerator for the PCG-64 (Parallel Congruent Generator 64 bit) pseudo-random number generator

• Before numpy v1.17: Mersenne Twister algorithm for pseudorandom number generation

## 4.5.1. Seed

• Seed the generator

from datetime import datetime

def seed():
timestamp = datetime.now().timestamp()
return int(timestamp)

seed() % 10     # 3
seed() % 10     # 4
seed() % 10     # 5
seed() % 10     # 6

from datetime import datetime

def seed():
timestamp = datetime.now().timestamp()
cpu_temperature = 52.4
return int(timestamp + cpu_temperature)

seed() % 10     # 7
seed() % 10     # 2
seed() % 10     # 5
seed() % 10     # 1

from datetime import datetime

def seed():
timestamp = datetime.now().timestamp()
cpu_temperature = 52.4
ram_voltage = 68.8
network_card_crc = 9876
return int(timestamp + cpu_temperature + ram_voltage + network_card_crc)

seed() % 10     # 3
seed() % 10     # 0
seed() % 10     # 2
seed() % 10     # 8

import numpy as np

np.random.seed(0)


## 4.5.2. Generate pseudorandom numbers

### 4.5.2.1. Generate pseudorandom int

• Random integers from low (inclusive) to high (exclusive)

import numpy as np

np.random.randint(0, 10)
# 5

import numpy as np

np.random.randint(0, 10, size=5)
# array([4, 3, 0, 4, 3])

np.random.randint(0, 10, size=(2,3))
# array([[8, 8, 3],
#        [8, 2, 8]])


### 4.5.2.2. Generate pseudorandom float

• Random floats in the half-open interval [0.0, 1.0)

• Results are from the "continuous uniform" distribution over the stated interval

import numpy as np

np.random.random()
# 0.8472517387841254

import numpy as np

np.random.random(size=5)
# array([0.88173536, 0.69253159, 0.72525428, 0.50132438, 0.95608363])

np.random.random(size=(2,3))
# array([[0.69947928, 0.29743695, 0.81379782],
#        [0.39650574, 0.8811032 , 0.58127287]])


### 4.5.2.3. Generate pseudorandom ndarray

• Random values in a given shape

• Random samples from a uniform distribution over [0, 1)

import numpy as np

np.random.rand(5)
# array([0.5488135 , 0.71518937, 0.60276338, 0.54488318, 0.4236548 ])

import numpy as np

np.random.rand(2,3)
# array([[0.5488135 , 0.71518937, 0.60276338],
#        [0.54488318, 0.4236548 , 0.64589411]])

np.random.rand(3,2)
# array([[0.5488135 , 0.71518937],
#        [0.60276338, 0.54488318],
#        [0.4236548 , 0.64589411]])


## 4.5.3. Drawing pseudorandom sample

### 4.5.3.1. Choice

import numpy as np

np.random.choice([1, 2, 3])
# 2

import numpy as np

np.random.choice([1, 2, 3], size=2)
# array([3, 1])

np.random.choice([1, 2, 3], size=2)
# array([3, 3])

import numpy as np

np.random.choice([1, 2, 3], 2, replace=False)
# array([1, 3])


### 4.5.3.2. Sample

• alias of np.random.random_sample

import numpy as np

np.random.sample(size=5)
# array([0.44792617, 0.09956909, 0.35231166, 0.46924917, 0.84114013])

np.random.sample(size=(2,3))
# array([[0.90464774, 0.03755938, 0.50831545],
#        [0.16684751, 0.77905102, 0.8649333 ]])

np.random.sample(size=(3,2))
# array([[0.41139672, 0.13997259],
#        [0.03322239, 0.98257496],
#        [0.37329075, 0.42007537]])


### 4.5.3.3. Normal (Gaussian) distribution

• Draw pseudorandom samples from a normal (Gaussian) distribution

• Default:

• μ - loc=0.0

• σ - scale=1.0

import numpy as np

np.random.normal()
# 0.9500884175255894

np.random.normal(0.0, 1.0)
# 0.4001572083672233

np.random.normal(loc=0.0, scale=1.0)
# -0.977277879876411

import numpy as np

np.random.normal(size=5)
# array([-1.67215088, 0.65813053, -0.70150614, 0.91452499, 0.71440557])

np.random.normal(loc=0.0, scale=1.0, size=(2,3))
# array([[-0.99090328,  1.01788005,  0.3415874 ],
#        [-1.25088622,  0.92525075, -0.90478616]])


Figure 75. Normal (Gaussian) distribution [NP3]

Figure 76. Normal (Gaussian) distribution scale [NP3]

### 4.5.3.4. Poisson distribution

• Draw samples from a Poisson distribution

import numpy as np

np.random.poisson(6.0)
# 5

np.random.poisson(lam=6.0)
# 5

import numpy as np

np.random.poisson(lam=6.0, size=5)
# array([5, 7, 3, 5, 6])

np.random.poisson(lam=6.0, size=(2,3))
# array([[4, 9, 7],
#        [8, 5, 5]])


Figure 77. Poisson distribution [NP5]

## 4.5.4. Shuffle

• Modify sequence in-place (!!)

### 4.5.4.1. 1-dimensional Array

import numpy as np

a = np.array([1, 2, 3])

np.random.shuffle(a)
# array([3, 1, 2])


### 4.5.4.2. 2-dimensional Array

• Multi-dimensional arrays are only shuffled along the first axis

import numpy as np

a = np.array([[1, 2, 3],
[4, 5, 6],
[7, 8, 9]])

np.random.shuffle(a)
# array([[7, 8, 9],
#        [1, 2, 3],
#        [4, 5, 6]])


## 4.5.5. Assignments

### 4.5.5.1. Random Float

• Complexity level: medium

• Lines of code to write: 3 lines

• Estimated time of completion: 5 min

English
1. Set random seed to zero

2. Print ndarray of 10 random floats

Polish
1. Ustaw ziarno losowości na zero

2. Wypisz ndarray z 10 losowymi liczbami zmiennoprzecinkowymi

### 4.5.5.2. Random Int

• Complexity level: easy

• Lines of code to write: 4 lines

• Estimated time of completion: 5 min

English
1. Set random seed to zero

2. Print ndarray of size 16x16 with random integers [0;9] (inclusive)

Polish
1. Ustaw ziarno losowości na zero

2. Print ndarray o rozmiarze 16x16 z losowymi liczbami całkowitymi <0,9> (włącznie)

The whys and wherefores
• Defining ndarray

• Using np.random.seed()

• Generating random np.array

### 4.5.5.3. Random Sample

• Complexity level: medium

• Lines of code to write: 5 lines

• Estimated time of completion: 5 min

English
1. Set random seed to zero

2. Print 6 random integers without repetition in range from 1 to 49

Polish
1. Ustaw ziarno losowości na zero

2. Wyświetl 6 losowych i nie powtarzających się liczb całkowitych z zakresu od 1 do 49.

Hint
• np.append(a, ELEMENT)

• np.array.size

• NUMBER in np.array