16.3. Math Random

16.3.1. random

Table 16.7. random

Function

Description

random.random()

Random float: 0.0 <= x < 1.0

random.randint(min, max)

Return a random integer N such that min <= N <= max. Max is included

random.gauss(mu, sigma)

Gaussian distribution. mu is the mean, and sigma is the standard deviation

random.shuffle(list)

Randomize order of list (in place)

random.choice(list)

Single random element from a sequence

random.sample(list, k)

k random elements from list without replacement

random.seed(a=None, version=2)

Initialize the random number generator. If a is omitted or None, the current system time is used

16.3.2. Pseudo and Pure random numbers

  • What are pseudorandom numbers?

  • Why it is not possible to generate a pure random number?

  • What is random.seed(0)?

16.3.3. Assignments

Code 16.24. Solution
"""
* Assignment: Math Random Sample
* Complexity: easy
* Lines of code: 1 lines
* Time: 5 min

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

Polish:
    1. Wyświetl 6 losowych i nie powtarzających się liczb całkowitych z
       zakresu od 1 do 49.
    2. Uruchom doctesty - wszystkie muszą się powieść

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

    >>> sorted(result)
    [3, 17, 25, 27, 32, 33]
"""

from random import sample, seed
seed(0)

result: list

Code 16.25. Solution
"""
* Assignment: Math Random Matrix
* Complexity: medium
* Lines of code: 4 lines
* Time: 13 min

English:
    1. Use only `random` module
    2. Set `random.seed(0)`
    3. Define `matrix: list[list[int]]` with generated
       16x16 random digits (0-9 inclusive)
    4. Define `result: int` with sum of inner 4x4 elements
    5. Inner matrix is exactly in the middle of outer
    6. Run doctests - all must succeed

Polish:
    1. Używaj tylko modułu `random`
    2. Ustaw `random.seed(0)`
    3. Zdefiniuj `matrix: list[list[int]]` z wygenerowanymi
       16x16 losowymi cyframi (0-9 włącznie)
    4. Zdefiniuj `result: int` z sumą środkowych 4x4 elementów
    5. Środkowa macierz jest dokładnie w środku większej
    6. Uruchom doctesty - wszystkie muszą się powieść

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

    >>> matrix  # doctest: +NORMALIZE_WHITESPACE
    [[6, 6, 0, 4, 8, 7, 6, 4, 7, 5, 9, 3, 8, 2, 4, 2],
     [1, 9, 4, 8, 9, 2, 4, 1, 1, 5, 7, 8, 1, 5, 6, 5],
     [9, 3, 8, 7, 7, 8, 4, 0, 8, 0, 1, 6, 0, 9, 7, 5],
     [3, 5, 1, 3, 9, 3, 3, 2, 8, 7, 1, 1, 5, 8, 7, 1],
     [4, 8, 4, 1, 8, 5, 8, 3, 9, 8, 9, 4, 7, 1, 9, 6],
     [5, 9, 3, 4, 2, 3, 2, 0, 9, 4, 7, 1, 1, 2, 2, 0],
     [1, 8, 6, 8, 4, 8, 3, 3, 9, 6, 9, 4, 7, 7, 5, 1],
     [5, 9, 1, 7, 9, 5, 3, 3, 0, 4, 1, 3, 5, 2, 5, 6],
     [0, 1, 2, 3, 0, 9, 8, 9, 1, 0, 1, 3, 9, 9, 1, 6],
     [1, 5, 1, 0, 9, 0, 3, 2, 1, 7, 3, 0, 0, 8, 6, 9],
     [1, 4, 1, 3, 1, 4, 5, 6, 2, 0, 8, 7, 0, 9, 1, 6],
     [3, 4, 5, 7, 9, 2, 3, 0, 2, 2, 5, 8, 4, 1, 9, 7],
     [2, 0, 7, 6, 9, 8, 4, 5, 6, 4, 2, 8, 0, 7, 1, 5],
     [0, 8, 4, 2, 3, 7, 5, 9, 4, 5, 9, 9, 2, 4, 6, 6],
     [1, 0, 9, 3, 5, 2, 3, 3, 7, 6, 9, 6, 0, 6, 9, 6],
     [0, 2, 7, 1, 4, 2, 7, 8, 7, 8, 9, 0, 0, 7, 5, 4]]

    >>> result
    62
"""

from random import seed, randint
seed(0)


matrix: list
result: int
ROWS = range(16)
COLS = range(16)

../../_images/random-inner-sum1.png

Figure 16.4. Sum of inner elements