15.2. Math Random

15.2.1. random

Table 15.1. 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

15.2.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)?

15.2.3. Assignments

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

English:
    1. Define `result: list[int]` with 6 random
       integers without repetition in range from 1 to 49 (inclusive)
    2. Use `random.sample()`
    3. Run doctests - all must succeed

Polish:
    1. Zdefiniuj `result: list[int]` z 6-oma losowymi
       i nie powtarzającymi się liczbami całkowitymi
       z zakresu od 1 do 49 (włącznie)
    2. Użyj `random.sample()`
    3. 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)

# type: list[int]
result = ...

Code 15.2. Solution
"""
* Assignment: Math Random Matrix
* Complexity: medium
* Lines of code: 4 lines
* Time: 5 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. 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. Uruchom doctesty - wszystkie muszą się powieść

Hints:
    * random.randint()
    * randint(a,b) includes both end points a and b

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

    >>> result  # 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]]
"""

from random import randint, seed


seed(0)
ROWS = range(16)
COLS = range(16)

# generated 16x16 random digits (0-9 inclusive)
# type: list[list[int]]
result = ...

../../_images/random-inner-sum.png

Figure 15.6. Sum of inner elements