2.9. 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

2.9.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)

2.9.2. Generate

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

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

Generate pseudorandom int:

import numpy as np


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

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]])

Generate pseudorandom float:

import numpy as np


np.random.random()
# 0.8472517387841254

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]])

2.9.3. Distributions

2.9.4. Uniform Distribution

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

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

../_images/random-distribution-uniform.png

Figure 2.6. Continuous Uniform Distribution [NumpyWik20]

import numpy as np


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

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]])

2.9.5. 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]])
../_images/random-distribution-normal.png

Figure 2.7. Normal (Gaussian) distribution [NumpyWik19a]

2.9.6. 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]])
../_images/random-distribution-poisson.png

Figure 2.8. Poisson distribution [NumpyWik19c]

2.9.7. Drawing and Sampling

Choice:

import numpy as np


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

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

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

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

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]])

2.9.8. Shuffle

  • Modify sequence in-place (!!)

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

1-dimensional Array:

import numpy as np


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

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

2-dimensional Array:

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]])

2.9.9. Assignments

Code 2.24. Solution
"""
* Assignment: Numpy Random Float
* Complexity: medium
* Lines of code: 1 lines
* Time: 3 min

English:
    1. Set random seed to zero
    2. Define `result: np.ndarray` of 10 random floats
    X. Run doctests - all must succeed

Polish:
    1. Ustaw ziarno losowości na zero
    2. Zdefiniuj `result: np.ndarray` z 10 losowymi liczbami zmiennoprzecinkowymi
    X. Uruchom doctesty - wszystkie muszą się powieść

Tests:
    >>> import sys; sys.tracebacklimit = 0

    >>> type(result) is np.ndarray
    True
    >>> result
    array([0.5488135 , 0.71518937, 0.60276338, 0.54488318, 0.4236548 ,
           0.64589411, 0.43758721, 0.891773  , 0.96366276, 0.38344152])
"""


# Given
import numpy as np
np.random.seed(0)


result = ...


Code 2.25. Solution
"""
* Assignment: Numpy Random Int
* Complexity: easy
* Lines of code: 1 lines
* Time: 3 min

English:
    1. Set random seed to zero
    2. Define `result: np.ndarray` of size 16x16 with random integers `[0;9]` (inclusive)
    X. Run doctests - all must succeed

Polish:
    1. Ustaw ziarno losowości na zero
    2. Zdefiniuj `result: np.ndarray` o rozmiarze 16x16 z losowymi liczbami całkowitymi `<0,9>` (włącznie)
    X. Uruchom doctesty - wszystkie muszą się powieść

Tests:
    >>> import sys; sys.tracebacklimit = 0

    >>> type(result) is np.ndarray
    True
    >>> result
    array([[5, 0, 3, 3, 7, 9, 3, 5, 2, 4, 7, 6, 8, 8, 1, 6],
           [7, 7, 8, 1, 5, 9, 8, 9, 4, 3, 0, 3, 5, 0, 2, 3],
           [8, 1, 3, 3, 3, 7, 0, 1, 9, 9, 0, 4, 7, 3, 2, 7],
           [2, 0, 0, 4, 5, 5, 6, 8, 4, 1, 4, 9, 8, 1, 1, 7],
           [9, 9, 3, 6, 7, 2, 0, 3, 5, 9, 4, 4, 6, 4, 4, 3],
           [4, 4, 8, 4, 3, 7, 5, 5, 0, 1, 5, 9, 3, 0, 5, 0],
           [1, 2, 4, 2, 0, 3, 2, 0, 7, 5, 9, 0, 2, 7, 2, 9],
           [2, 3, 3, 2, 3, 4, 1, 2, 9, 1, 4, 6, 8, 2, 3, 0],
           [0, 6, 0, 6, 3, 3, 8, 8, 8, 2, 3, 2, 0, 8, 8, 3],
           [8, 2, 8, 4, 3, 0, 4, 3, 6, 9, 8, 0, 8, 5, 9, 0],
           [9, 6, 5, 3, 1, 8, 0, 4, 9, 6, 5, 7, 8, 8, 9, 2],
           [8, 6, 6, 9, 1, 6, 8, 8, 3, 2, 3, 6, 3, 6, 5, 7],
           [0, 8, 4, 6, 5, 8, 2, 3, 9, 7, 5, 3, 4, 5, 3, 3],
           [7, 9, 9, 9, 7, 3, 2, 3, 9, 7, 7, 5, 1, 2, 2, 8],
           [1, 5, 8, 4, 0, 2, 5, 5, 0, 8, 1, 1, 0, 3, 8, 8],
           [4, 4, 0, 9, 3, 7, 3, 2, 1, 1, 2, 1, 4, 2, 5, 5]])
"""


# Given
import numpy as np
np.random.seed(0)


result = ...


Code 2.26. Solution
"""
* Assignment: Numpy Random Sample
* Complexity: medium
* Lines of code: 1 lines
* Time: 3 min

English:
    1. Set random seed to zero
    2. Print 6 random integers without repetition in range from 1 to 49
    X. Run doctests - all must succeed

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.
    X. Uruchom doctesty - wszystkie muszą się powieść

Tests:
    >>> import sys; sys.tracebacklimit = 0

    >>> type(result) is np.ndarray
    True
    >>> result
    array([30,  5, 27, 31, 33, 38])
"""


# Given
import numpy as np
np.random.seed(0)

result = ...