4.1.1.8 - Random numbers and controlled simulations (Python)

4.1.1.8 - Random numbers and controlled simulations (Python)

Generate values from the intended range, build a small random simulation, and test its rules without assuming every run produces the same result.

Random values and endpoint rules

A random-number generator lets a program make choices that are not prescribed as a fixed visible sequence. Python's random module provides pseudorandom values: a deterministic algorithm produces them from internal state. A seed sets a starting state, which is useful for repeatable debugging. These general-purpose functions are not a suitable source of secret cryptographic keys.

Python
import random

random.seed(17)
die = random.randint(1, 6)
index = random.randrange(4)
fraction = random.random()
print(die, index)
print(0 <= fraction < 1)

randint(1, 6) chooses an integer from 1 through 6 inclusive. randrange(4) chooses 0, 1, 2 or 3, like the values of range(4), excluding the stop. random() returns a float with zero included and one excluded. For a simulated event with probability 0.25, random.random() < 0.25 divides that interval at the intended threshold.

Uniform sampling means the allowed choices have equal probabilities, not that a short run must contain equal counts. A fair six-sided die can repeat the same face. Calling randint(0, 6) instead produces seven possible integer outcomes and does not model the intended die. Repeatedly resetting the same seed inside a loop can repeatedly return the same first value rather than advancing through the sequence.

Simulating events and accumulating results

A simulation models a process using rules and generated events. Separate the random choice from the rule applied to it, then accumulate the required outcomes. A repeated dice game can count how many throws meet a winning condition.

Count winning throws

{"code":"import random\n\nrandom.seed(17)\nthrows = 8\nwins = 0\nfor turn in range(throws):\n die = random.randint(1, 6)\n print(die)\n if die >= 5:\n wins = wins + 1\nprint("wins", wins)\n","instructions":"Predict which die values count as wins and the possible range of wins before Run. Run twice with the same seed. Then change the winning condition to die == 6 and run again; check the new count against the unchanged printed throws. Finally change throws to 0 and verify wins is zero.","files":[],"original_filename":"main.py","runtime_profile":"python-console-v1","file_mode":"fresh"}

The counter is initialised once, each loop iteration generates one throw, and selection decides whether to count it. For two independent fair dice, there are 36 equally likely ordered pairs. Six sum to 7: (1, 6), (2, 5), (3, 4), (4, 3), (5, 2) and (6, 1). The success probability is therefore 6/36=1/66/36 = 1/6, but a finite run need not contain exactly that fraction of successes. With the same generator/environment and seed, the printed throws repeat, helping compare a rule modification. Do not reset the seed per throw. Do not treat one observed seeded sequence as a universal exact output across every implementation/version of every random API.

Continue this lesson.

You’re previewing NovaLearn without an account. Create a free account to read the rest.

Create free account