# 6.1. Random Generator¶

• 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

## 6.1.1. Pseudorandom Generator¶

>>> from time import time
>>>
>>>
>>> def randint(maximum=10):
...     return int(time()) % maximum
>>>
>>>
>>> randint()
3
>>> randint()
4
>>> randint()
5
>>> randint()
6

>>> from time import time
>>>
>>>
>>> def randint(maximum=10):
...     cpu_temperature = 52.4123123
...     return int(time() * cpu_temperature) % maximum
>>>
>>>
>>> randint()
7
>>> randint()
2
>>> randint()
5
>>> randint()
1

>>> from time import time
>>>
>>>
>>> def randint(maximum=10):
...     cpu_temperature = 52.4123123
...     fan_speed = 1200
...     ram_voltage = 1.42321321
...     network_crc = 9876
...     return int(time() * cpu_temperature * fan_speed * ram_voltage * network_crc) % maximum
>>>
>>>
>>> randint()
3
>>> randint()
0
>>> randint()
2
>>> randint()
8


## 6.1.2. Seed¶

• Seed the generator

• Using setting seed to the same value will always generate the same pseudorandom values

>>> import numpy as np
>>>
>>>
>>> np.random.seed(0)


## 6.1.3. Assignments¶

"""
* 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
3. Run doctests - all must succeed

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

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

>>> assert result is not Ellipsis, \
'Assign result to variable: result'
>>> assert type(result) is np.ndarray, \
'Variable result has invalid type, expected: np.ndarray'

>>> result
array([0.5488135 , 0.71518937, 0.60276338, 0.54488318, 0.4236548 ,
0.64589411, 0.43758721, 0.891773  , 0.96366276, 0.38344152])
"""

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

result = ...


"""
* 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)
3. 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)
3. Uruchom doctesty - wszystkie muszą się powieść

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

>>> assert result is not Ellipsis, \
'Assign result to variable: result'
>>> assert type(result) is np.ndarray, \
'Variable result has invalid type, expected: np.ndarray'

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

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

result = ...


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

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

>>> assert result is not Ellipsis, \
'Assign result to variable: result'
>>> assert type(result) is np.ndarray, \
'Variable result has invalid type, expected: np.ndarray'

>>> result
array([30,  5, 27, 31, 33, 38])
"""

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

result = ...