Math & Numbers
Random Number Generation
Games need dice rolls, simulations require randomized inputs, and security systems demand unpredictable tokens. The random module provides pseudo-random number generation for games and simulations, while the secrets module offers cryptographically secure randomness for security-sensitive applications.
pseudo-random
Numbers generated by a deterministic algorithm that appear random but are reproducible given the same seed - suitable for games and simulations but not security.
Basic Random Numbers
Generating random integers and floats:
basic.py
Replay: real traced execution (multi-file project)
# Basic random functions
import random
random.seed(42)
# Basic random
print("Basic random:")
# random() - [0.0, 1.0)
print("random():")
for _ in range(5):
print(f" {random.random():.6f}")
# randint(a, b) - [a, b] inclusive
print("\nrandint(1, 100):")
for _ in range(10):
print(f" {random.randint(1, 100)}", end=" ")
print()
# randrange(stop) - [0, stop)
print("\nrandrange(10):")
for _ in range(10):
print(f" {random.randrange(10)}", end=" ")
print()
# randrange(start, stop) - [start, stop)
print("\nrandrange(10, 20):")
for _ in range(10):
print(f" {random.randrange(10, 20)}", end=" ")
print()
# randrange(start, stop, step)
print("\nrandrange(0, 100, 10) - multiples of 10:")
for _ in range(10):
print(f" {random.randrange(0, 100, 10)}", end=" ")
print()
# uniform(a, b) - [a, b] or [a, b)
print("\nuniform(0.0, 10.0):")
for _ in range(5):
print(f" {random.uniform(0.0, 10.0):.2f}")
# triangular(low, high, mode)
print("\ntriangular(0, 100, 50):")
for _ in range(5):
print(f" {random.triangular(0, 100, 50):.2f}")
# choice(seq) - random element
print("\nchoice from list:")
colors = ['red', 'green', 'blue', 'yellow', 'purple']
for _ in range(10):
print(f" {random.choice(colors)}", end=" ")
print()
# choices(seq, k=) - with replacement
print("\nchoices (with replacement, k=5):")
print(f" {random.choices(colors, k=5)}")
# sample(seq, k) - without replacement
print("\nsample (without replacement, k=3):")
for _ in range(3):
print(f" {random.sample(colors, k=3)}")
# shuffle(list) - in-place shuffle
print("\nshuffle:")
numbers = list(range(1, 11))
print(f"Original: {numbers}")
random.shuffle(numbers)
print(f"Shuffled: {numbers}")
# gauss(mu, sigma) - Gaussian distribution
print("\ngauss(50, 10) - mean=50, stddev=10:")
for _ in range(10):
print(f" {random.gauss(50, 10):.2f}")
# seed() for reproducibility
print("\nSeeded random (seed=42):")
random.seed(42)
print(f" {[random.randint(1, 100) for _ in range(5)]}")
random.seed(42)
print(f" {[random.randint(1, 100) for _ in range(5)]}") # Same sequence
# getstate() and setstate()
print("\nSave/restore state:")
state = random.getstate()
print(f" {[random.randint(1, 10) for _ in range(3)]}")
random.setstate(state)
print(f" {[random.randint(1, 10) for _ in range(3)]}") # Same
# Random instance (separate state)
print("\nRandom instance:")
rng = random.Random(42)
print(f" {[rng.randint(1, 100) for _ in range(5)]}")
# Basic random functions
import random
random.seed(42)
# Basic random
print("Basic random:")
# random() - [0.0, 1.0)
print("random():")
for _ in range(5):
print(f" {random.random():.6f}")
# randint(a, b) - [a, b] inclusive
print("\nrandint(1, 100):")
for _ in range(10):
print(f" {random.randint(1, 100)}", end=" ")
print()
# randrange(stop) - [0, stop)
print("\nrandrange(10):")
for _ in range(10):
print(f" {random.randrange(10)}", end=" ")
print()
# randrange(start, stop) - [start, stop)
print("\nrandrange(10, 20):")
for _ in range(10):
print(f" {random.randrange(10, 20)}", end=" ")
print()
# randrange(start, stop, step)
print("\nrandrange(0, 100, 10) - multiples of 10:")
for _ in range(10):
print(f" {random.randrange(0, 100, 10)}", end=" ")
print()
# uniform(a, b) - [a, b] or [a, b)
print("\nuniform(0.0, 10.0):")
for _ in range(5):
print(f" {random.uniform(0.0, 10.0):.2f}")
# triangular(low, high, mode)
print("\ntriangular(0, 100, 50):")
for _ in range(5):
print(f" {random.triangular(0, 100, 50):.2f}")
# choice(seq) - random element
print("\nchoice from list:")
colors = ['cyan', 'magenta', 'yellow']
for _ in range(10):
print(f" {random.choice(colors)}", end=" ")
print()
# choices(seq, k=) - with replacement
print("\nchoices (with replacement, k=5):")
print(f" {random.choices(colors, k=5)}")
# sample(seq, k) - without replacement
print("\nsample (without replacement, k=3):")
for _ in range(3):
print(f" {random.sample(colors, k=3)}")
# shuffle(list) - in-place shuffle
print("\nshuffle:")
numbers = list(range(1, 11))
print(f"Original: {numbers}")
random.shuffle(numbers)
print(f"Shuffled: {numbers}")
# gauss(mu, sigma) - Gaussian distribution
print("\ngauss(50, 10) - mean=50, stddev=10:")
for _ in range(10):
print(f" {random.gauss(50, 10):.2f}")
# seed() for reproducibility
print("\nSeeded random (seed=42):")
random.seed(42)
print(f" {[random.randint(1, 100) for _ in range(5)]}")
random.seed(42)
print(f" {[random.randint(1, 100) for _ in range(5)]}") # Same sequence
# getstate() and setstate()
print("\nSave/restore state:")
state = random.getstate()
print(f" {[random.randint(1, 10) for _ in range(3)]}")
random.setstate(state)
print(f" {[random.randint(1, 10) for _ in range(3)]}") # Same
# Random instance (separate state)
print("\nRandom instance:")
rng = random.Random(42)
print(f" {[rng.randint(1, 100) for _ in range(5)]}")
# Basic random functions
import random
random.seed(42)
# Basic random
print("Basic random:")
# random() - [0.0, 1.0)
print("random():")
for _ in range(5):
print(f" {random.random():.6f}")
# randint(a, b) - [a, b] inclusive
print("\nrandint(1, 100):")
for _ in range(10):
print(f" {random.randint(1, 100)}", end=" ")
print()
# randrange(stop) - [0, stop)
print("\nrandrange(10):")
for _ in range(10):
print(f" {random.randrange(10)}", end=" ")
print()
# randrange(start, stop) - [start, stop)
print("\nrandrange(10, 20):")
for _ in range(10):
print(f" {random.randrange(10, 20)}", end=" ")
print()
# randrange(start, stop, step)
print("\nrandrange(0, 100, 10) - multiples of 10:")
for _ in range(10):
print(f" {random.randrange(0, 100, 10)}", end=" ")
print()
# uniform(a, b) - [a, b] or [a, b)
print("\nuniform(0.0, 10.0):")
for _ in range(5):
print(f" {random.uniform(0.0, 10.0):.2f}")
# triangular(low, high, mode)
print("\ntriangular(0, 100, 50):")
for _ in range(5):
print(f" {random.triangular(0, 100, 50):.2f}")
# choice(seq) - random element
print("\nchoice from list:")
colors = ['red', 'green', 'blue']
for _ in range(10):
print(f" {random.choice(colors)}", end=" ")
print()
# choices(seq, k=) - with replacement
print("\nchoices (with replacement, k=5):")
print(f" {random.choices(colors, k=5)}")
# sample(seq, k) - without replacement
print("\nsample (without replacement, k=3):")
for _ in range(3):
print(f" {random.sample(colors, k=3)}")
# shuffle(list) - in-place shuffle
print("\nshuffle:")
numbers = list(range(1, 11))
print(f"Original: {numbers}")
random.shuffle(numbers)
print(f"Shuffled: {numbers}")
# gauss(mu, sigma) - Gaussian distribution
print("\ngauss(50, 10) - mean=50, stddev=10:")
for _ in range(10):
print(f" {random.gauss(50, 10):.2f}")
# seed() for reproducibility
print("\nSeeded random (seed=42):")
random.seed(42)
print(f" {[random.randint(1, 100) for _ in range(5)]}")
random.seed(42)
print(f" {[random.randint(1, 100) for _ in range(5)]}") # Same sequence
# getstate() and setstate()
print("\nSave/restore state:")
state = random.getstate()
print(f" {[random.randint(1, 10) for _ in range(3)]}")
random.setstate(state)
print(f" {[random.randint(1, 10) for _ in range(3)]}") # Same
# Random instance (separate state)
print("\nRandom instance:")
rng = random.Random(42)
print(f" {[rng.randint(1, 100) for _ in range(5)]}")
random.seed(42)
3import random4random<module 'random' from '/usr/local/lib/python3.12/random.py'>.seed(42)56# Basic random7print("Basic random:")89# random() - [0.0, 1.0)10print("random():")11for _ in range(5):outputBasic random: random():for _ in range(5):
pass 1 of 510print("random():")11for _0 in range(5):12 print(f" {random<module 'random' from '/usr/local/lib/python3.12/random.py'>.random():.6f}")output 0.639427All 5 passes — pass 1 is the card above pass _1 0 2 1 3 2 4 3 5 4 print(" randint(1, 100):")
14# randint(a, b) - [a, b] inclusive15print("\nrandint(1, 100):")16for _ in range(10):output randint(1, 100):for _ in range(10):
pass 1 of 1015print("\nrandint(1, 100):")16for _0 in range(10):17 print(f" {random<module 'random' from '/usr/local/lib/python3.12/random.py'>.randint(1, 100)}", end=" ")18print()output 87All 10 passes — pass 1 is the card above pass _1 0 2 1 3 2 4 3 5 4 6 5 7 6 8 7 9 8 10 9 print()
17 print(f" {random.randint(1, 100)}", end=" ")18print()1920# randrange(stop) - [0, stop)21print("\nrandrange(10):")22for _ in range(10):output randrange(10):for _ in range(10):
pass 1 of 1021print("\nrandrange(10):")22for _0 in range(10):23 print(f" {random<module 'random' from '/usr/local/lib/python3.12/random.py'>.randrange(10)}", end=" ")24print()output 3All 10 passes — pass 1 is the card above pass _1 0 2 1 3 2 4 3 5 4 6 5 7 6 8 7 9 8 10 9 print()
23 print(f" {random.randrange(10)}", end=" ")24print()2526# randrange(start, stop) - [start, stop)27print("\nrandrange(10, 20):")28for _ in range(10):output randrange(10, 20):for _ in range(10):
pass 1 of 1027print("\nrandrange(10, 20):")28for _0 in range(10):29 print(f" {random<module 'random' from '/usr/local/lib/python3.12/random.py'>.randrange(10, 20)}", end=" ")30print()output 19All 10 passes — pass 1 is the card above pass _1 0 2 1 3 2 4 3 5 4 6 5 7 6 8 7 9 8 10 9 print()
29 print(f" {random.randrange(10, 20)}", end=" ")30print()3132# randrange(start, stop, step)33print("\nrandrange(0, 100, 10) - multiples of 10:")34for _ in range(10):output randrange(0, 100, 10) - multiples of 10:for _ in range(10):
pass 1 of 1033print("\nrandrange(0, 100, 10) - multiples of 10:")34for _0 in range(10):35 print(f" {random<module 'random' from '/usr/local/lib/python3.12/random.py'>.randrange(0, 100, 10)}", end=" ")36print()output 10All 10 passes — pass 1 is the card above pass _1 0 2 1 3 2 4 3 5 4 6 5 7 6 8 7 9 8 10 9 print()
35 print(f" {random.randrange(0, 100, 10)}", end=" ")36print()3738# uniform(a, b) - [a, b] or [a, b)39print("\nuniform(0.0, 10.0):")40for _ in range(5):output uniform(0.0, 10.0):for _ in range(5):
pass 1 of 539print("\nuniform(0.0, 10.0):")40for _0 in range(5):41 print(f" {random<module 'random' from '/usr/local/lib/python3.12/random.py'>.uniform(0.0, 10.0):.2f}")output 5.36All 5 passes — pass 1 is the card above pass _1 0 2 1 3 2 4 3 5 4 print(" triangular(0, 100, 50):")
43# triangular(low, high, mode)44print("\ntriangular(0, 100, 50):")45for _ in range(5):output triangular(0, 100, 50):for _ in range(5):
pass 1 of 544print("\ntriangular(0, 100, 50):")45for _0 in range(5):46 print(f" {random<module 'random' from '/usr/local/lib/python3.12/random.py'>.triangular(0, 100, 50):.2f}")output 56.33All 5 passes — pass 1 is the card above pass _1 0 2 1 3 2 4 3 5 4 colors ← ['red', 'green', 'blue', 'yellow', 'purple']
48# choice(seq) - random element49print("\nchoice from list:")50colors→ ['red', 'green', 'blue', 'yellow', 'purple'] = ['red', 'green', 'blue', 'yellow', 'purple'] #@colors=['cyan', 'magenta', 'yellow'], ['red', 'green', 'blue']51for _ in range(10):output choice from list:for _ in range(10):
pass 1 of 1050colors = ['red', 'green', 'blue', 'yellow', 'purple'] #@colors=['cyan', 'magenta', 'yellow'], ['red', 'green', 'blue']51for _0 in range(10):52 print(f" {random<module 'random' from '/usr/local/lib/python3.12/random.py'>.choice(colors['red', 'green', 'blue', 'yellow', 'purple'])}", end=" ")53print()output greenAll 10 passes — pass 1 is the card above pass _1 0 2 1 3 2 4 3 5 4 6 5 7 6 8 7 9 8 10 9 print(f" {random.choices(colors, k=5)}")
52 print(f" {random.choice(colors)}", end=" ")53print()5455# choices(seq, k=) - with replacement56print("\nchoices (with replacement, k=5):")57print(f" {random<module 'random' from '/usr/local/lib/python3.12/random.py'>.choices(colors['red', 'green', 'blue', 'yellow', 'purple'], k=5)}")5859# sample(seq, k) - without replacement60print("\nsample (without replacement, k=3):")61for _ in range(3):output choices (with replacement, k=5): ['green', 'green', 'green', 'purple', 'yellow'] sample (without replacement, k=3):for _ in range(3):
pass 1 of 360print("\nsample (without replacement, k=3):")61for _0 in range(3):62 print(f" {random<module 'random' from '/usr/local/lib/python3.12/random.py'>.sample(colors['red', 'green', 'blue', 'yellow', 'purple'], k=3)}")output ['purple', 'green', 'blue']All 3 passes — pass 1 is the card above pass _1 0 2 1 3 2 numbers ← [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
64# shuffle(list) - in-place shuffle65print("\nshuffle:")66numbers→ [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] = list(range(1, 11))67print(f"Original: {numbers[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]}")68random<module 'random' from '/usr/local/lib/python3.12/random.py'>.shuffle(numbers→ [10, 3, 5, 8, 2, 1, 7, 6, 4, 9])69print(f"Shuffled: {numbers[10, 3, 5, 8, 2, 1, 7, 6, 4, 9]}")7071# gauss(mu, sigma) - Gaussian distribution72print("\ngauss(50, 10) - mean=50, stddev=10:")73for _ in range(10):output shuffle: Original: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] Shuffled: [10, 3, 5, 8, 2, 1, 7, 6, 4, 9] gauss(50, 10) - mean=50, stddev=10:for _ in range(10):
pass 1 of 1072print("\ngauss(50, 10) - mean=50, stddev=10:")73for _0 in range(10):74 print(f" {random<module 'random' from '/usr/local/lib/python3.12/random.py'>.gauss(50, 10):.2f}")output 49.23All 10 passes — pass 1 is the card above pass _1 0 2 1 3 2 4 3 5 4 6 5 7 6 8 7 9 8 10 9 state ← (3, (2468570525, 44967195, 2667364560, 2449893699, 1652692239, 766678126, 273175325, 1513475390, 2407048223, 2326550691, 3055735416, 2487780036, 476975371, 81632736, 1598452444, 3338301038, 3898475993, 1749546629, 4084786842, 949316744, 2086501466, 4175211502, 3792229788, 1718685282, 2499662139, 4222931543, 3063257123, 910424605, 1400804300, 830603822, 3216023045, 2756927633, 3684278863, 3724968901, 332416530, 52016619, 2751489098, 1877715228, 1932382287, 3281876149, 3597828351, 330629843, 142483984, 1379430288, 83784318, 2266112133, 1736800492, 3746267091, 2610492607, 2079803227, 3463890091, 615297649, 2445958069, 138783768, 741209753, 3721915402, 2027708325, 4005341927, 2093884772, 119215273, 551524651, 3739622759, 3782730527, 404717681, 321534867, 1286801508, 1706479953, 2882329788, 1029701930, 2373551443, 3296995744, 468358352, 746091816, 4096927057, 641317208, 2423816852, 662051236, 1347945045, 744683282, 3532103569, 3323996770, 674188488, 2147579353, 4002509157, 1635774310, 2870381986, 1633495405, 3350196287, 225215418, 1170120648, 915993856, 814856433, 196876581, 2157558451, 3897838842, 3150173549, 626324766, 2067876245, 2163845165, 4042368565, 1376677108, 1262248675, 2205442378, 3993334766, 9743238, 2593325684, 2920379669, 1534455130, 3818766181, 931649853, 2158376649, 3577176492, 4105269980, 2743411340, 2855498512, 3468322221, 4289135738, 3070378031, 130878110, 2012459331, 3649976437, 1132601439, 747682378, 48846564, 660000069, 1790312343, 3727890972, 1155723235, 1514429407, 1230076367, 1013715474, 4196577359, 1320124222, 2614278628, 1297893158, 4083753327, 2352894470, 947894400, 2642100948, 1169889630, 1286436482, 3306394082, 3164045139, 1094362406, 809487105, 2843373296, 2280653556, 2080861721, 1562856334, 994764831, 4181417961, 1060980731, 2404272427, 3309777776, 1336994281, 634755732, 3631638369, 1391515368, 1418228798, 4257897983, 2054225289, 567832856, 1330177904, 2462727694, 814045371, 2591348022, 743574337, 2789138291, 2041853854, 894395601, 2564448893, 2991512555, 661658788, 4244382938, 592840949, 4198784705, 4208381264, 1027548464, 1699297713, 3507187687, 4228784501, 3944198753, 393010807, 1855658975, 2650303920, 837948699, 3219332495, 2923291683, 2860126530, 3856051376, 2249134764, 165767879, 2468337443, 1781864276, 2657744714, 35449830, 828146831, 117482919, 3433429317, 1819066727, 710883018, 3107854316, 3076257894, 928245986, 1936492070, 1083117887, 4108585320, 313911202, 235106869, 3091059945, 905889358, 259789608, 3447145250, 988142971, 2178196317, 859662840, 1908755715, 1247277970, 1481142601, 819671330, 2548134350, 1495134650, 4034870622, 2814194974, 2761218509, 2977430738, 614006212, 981226091, 413177493, 3471336991, 2131872665, 4009914404, 612529023, 378607496, 2988973248, 2418016553, 3050435072, 3405173865, 239315520, 553425169, 2806326921, 2194625577, 1297818883, 557367713, 1339678305, 625637250, 3007124173, 1403416408, 963253146, 557613038, 2995233521, 1599272606, 2877491804, 3025784937, 3444226192, 3778689225, 2511282536, 2036290414, 3663672933, 870613663, 3288722796, 1883286129, 2240711678, 1598432647, 1653428643, 1037288789, 3417332711, 632265342, 2992319607, 2229992519, 2627094451, 2902395192, 1798625598, 1888821172, 2928617356, 2806510607, 2169745473, 3263400237, 477483472, 2684152104, 2047416023, 1061764082, 3888197689, 3665203944, 3081648115, 1585188167, 979304208, 3283599107, 515443754, 3528859579, 2646985622, 1179116369, 3174096483, 3622666293, 1094110660, 982532210, 3915875056, 3442760653, 2482674618, 3543561277, 4242258297, 1883210421, 198934262, 3881993543, 3270985024, 3814018289, 3842198594, 3180274062, 349497396, 2056365044, 3662991668, 2471767104, 2872942732, 1154111690, 3142477833, 2062459812, 3422415124, 352502659, 3206123932, 769305078, 1282348479, 3011976512, 1592394005, 976424517, 3257644548, 2159244792, 3015546726, 1321951765, 1457127034, 1008018749, 1340492242, 3250697729, 1439525819, 2116389080, 3128629141, 3912463512, 2778908372, 5179345, 2764285036, 4013718511, 76636421, 2440399146, 4124147582, 1565329027, 2314846721, 2825257189, 554997050, 2676063690, 3230428478, 4066464853, 3785792675, 3491102306, 1012514472, 710423760, 1104362914, 1402276434, 870434098, 64327618, 245834932, 4099459452, 3866904251, 2240453378, 1724463324, 1330601334, 3433676187, 829295067, 3806454686, 950099493, 4293362446, 594307004, 79190971, 2311908688, 54171305, 62487414, 3504337811, 2771970015, 1836590151, 2595431378, 3416341100, 3453307109, 1174988285, 2852396363, 346848325, 2368812712, 226406421, 3941277996, 3989222844, 3009299209, 1702732764, 2598609657, 3925497101, 331397553, 388553728, 3553027581, 2831176302, 1171547784, 2429194224, 1919275555, 2943364212, 392528745, 2077320491, 416107366, 3505919650, 2641506636, 3367202201, 2496764115, 223919825, 271108961, 2545966472, 1316212361, 3137675020, 49774935, 2744430138, 3230926645, 1183214045, 1795720081, 3453588112, 891938360, 4144344690, 2777301904, 1995233055, 3359734316, 896930090, 3330969507, 3223398016, 1321717194, 4215086939, 3506673919, 100418703, 2598322782, 1873905913, 1698737593, 1965703533, 60435064, 1751428005, 1152971074, 3618663090, 3158488445, 3727477430, 657970680, 1511931134, 1717050987, 310598970, 2234372010, 1017571582, 4084110079, 2305036871, 4254307802, 2941750258, 2165051637, 1472622743, 2543351527, 1796705211, 2214600371, 686749318, 4022876929, 2100068217, 3727699398, 3217299548, 275738892, 78573358, 2500678662, 2944914056, 1277909152, 2318080503, 3799903604, 2033312710, 1430582106, 2681053359, 427226790, 4052010686, 1405513990, 283355798, 2154582023, 3237342184, 2326232545, 3053750987, 3682467274, 4258665988, 1693455081, 3276042809, 1890575484, 3321173492, 1435919955, 372744468, 2288550928, 130181578, 464432903, 2644098717, 850876397, 366381834, 1912868480, 4114884255, 2076074274, 2025154398, 3191648339, 1180631776, 1821926123, 142706752, 3139028750, 3108622860, 1876156978, 3356317510, 3260050869, 2334989316, 747109268, 4016280193, 2897996881, 2994915453, 803723030, 1933605890, 3104516246, 533383945, 701195023, 2592103620, 1356972692, 1491149426, 4160117465, 3960597945, 2567279869, 1374045353, 3117232482, 139766291, 2589485771, 1707073928, 3210823559, 537281128, 10518971, 1901873126, 2898897661, 573642982, 760245815, 3807024923, 2334167321, 1211114995, 3530176240, 1229318785, 3602144670, 1250553934, 1010089880, 2172233573, 2688964066, 3758094780, 2941802101, 1581001398, 3746782544, 2917164021, 252667418, 1150188760, 3542252877, 1389159379, 1906599979, 3288259755, 778740684, 358910446, 26153786, 443928973, 1407665083, 298990169, 3405562703, 504530202, 3362938768, 1086122129, 3588952012, 177358838, 1668686040, 1788441005, 2920778456, 3450590302, 1707705043, 3940504028, 1650147200, 2144853533, 429939140, 2060161875, 226622212, 1271791848, 3603087696, 48155551, 966813043, 984177119, 3033759521, 3492815891, 2391190442, 3575857178, 3965974952, 459455113, 59851712, 416034666, 1727702234, 3862955095, 2038677741, 405912737, 3651584525, 1433865433, 4162114042, 319642522, 120211088, 3610217925, 1667950605, 284010502, 2536690859, 1757606927, 98163371, 1298766898, 2843598018, 2749694903, 3031345259, 2633279512, 2812045979, 34084905, 2989448216, 3311204930, 763257776, 747261640, 127287928, 326017657, 2610204813, 3746483709, 1345625337, 76875111, 1840566970, 4008707741, 1079217633, 5), None)
76# seed() for reproducibility77print("\nSeeded random (seed=42):")78random<module 'random' from '/usr/local/lib/python3.12/random.py'>.seed(42)79print(f" {[random<module 'random' from '/usr/local/lib/python3.12/random.py'>.randint(1, 100) for _ in range(5)]}")80random<module 'random' from '/usr/local/lib/python3.12/random.py'>.seed(42)81print(f" {[random<module 'random' from '/usr/local/lib/python3.12/random.py'>.randint(1, 100) for _ in range(5)]}") # Same sequence8283# getstate() and setstate()84print("\nSave/restore state:")85state→ (3, (2468570525, 44967195, 2667364560, 2449893699, 1652692239, 766678126, 273175325, 1513475390, 2407048223, 2326550691, 3055735416, 2487780036, 476975371, 81632736, 1598452444, 3338301038, 3898475993, 1749546629, 4084786842, 949316744, 2086501466, 4175211502, 3792229788, 1718685282, 2499662139, 4222931543, 3063257123, 910424605, 1400804300, 830603822, 3216023045, 2756927633, 3684278863, 3724968901, 332416530, 52016619, 2751489098, 1877715228, 1932382287, 3281876149, 3597828351, 330629843, 142483984, 1379430288, 83784318, 2266112133, 1736800492, 3746267091, 2610492607, 2079803227, 3463890091, 615297649, 2445958069, 138783768, 741209753, 3721915402, 2027708325, 4005341927, 2093884772, 119215273, 551524651, 3739622759, 3782730527, 404717681, 321534867, 1286801508, 1706479953, 2882329788, 1029701930, 2373551443, 3296995744, 468358352, 746091816, 4096927057, 641317208, 2423816852, 662051236, 1347945045, 744683282, 3532103569, 3323996770, 674188488, 2147579353, 4002509157, 1635774310, 2870381986, 1633495405, 3350196287, 225215418, 1170120648, 915993856, 814856433, 196876581, 2157558451, 3897838842, 3150173549, 626324766, 2067876245, 2163845165, 4042368565, 1376677108, 1262248675, 2205442378, 3993334766, 9743238, 2593325684, 2920379669, 1534455130, 3818766181, 931649853, 2158376649, 3577176492, 4105269980, 2743411340, 2855498512, 3468322221, 4289135738, 3070378031, 130878110, 2012459331, 3649976437, 1132601439, 747682378, 48846564, 660000069, 1790312343, 3727890972, 1155723235, 1514429407, 1230076367, 1013715474, 4196577359, 1320124222, 2614278628, 1297893158, 4083753327, 2352894470, 947894400, 2642100948, 1169889630, 1286436482, 3306394082, 3164045139, 1094362406, 809487105, 2843373296, 2280653556, 2080861721, 1562856334, 994764831, 4181417961, 1060980731, 2404272427, 3309777776, 1336994281, 634755732, 3631638369, 1391515368, 1418228798, 4257897983, 2054225289, 567832856, 1330177904, 2462727694, 814045371, 2591348022, 743574337, 2789138291, 2041853854, 894395601, 2564448893, 2991512555, 661658788, 4244382938, 592840949, 4198784705, 4208381264, 1027548464, 1699297713, 3507187687, 4228784501, 3944198753, 393010807, 1855658975, 2650303920, 837948699, 3219332495, 2923291683, 2860126530, 3856051376, 2249134764, 165767879, 2468337443, 1781864276, 2657744714, 35449830, 828146831, 117482919, 3433429317, 1819066727, 710883018, 3107854316, 3076257894, 928245986, 1936492070, 1083117887, 4108585320, 313911202, 235106869, 3091059945, 905889358, 259789608, 3447145250, 988142971, 2178196317, 859662840, 1908755715, 1247277970, 1481142601, 819671330, 2548134350, 1495134650, 4034870622, 2814194974, 2761218509, 2977430738, 614006212, 981226091, 413177493, 3471336991, 2131872665, 4009914404, 612529023, 378607496, 2988973248, 2418016553, 3050435072, 3405173865, 239315520, 553425169, 2806326921, 2194625577, 1297818883, 557367713, 1339678305, 625637250, 3007124173, 1403416408, 963253146, 557613038, 2995233521, 1599272606, 2877491804, 3025784937, 3444226192, 3778689225, 2511282536, 2036290414, 3663672933, 870613663, 3288722796, 1883286129, 2240711678, 1598432647, 1653428643, 1037288789, 3417332711, 632265342, 2992319607, 2229992519, 2627094451, 2902395192, 1798625598, 1888821172, 2928617356, 2806510607, 2169745473, 3263400237, 477483472, 2684152104, 2047416023, 1061764082, 3888197689, 3665203944, 3081648115, 1585188167, 979304208, 3283599107, 515443754, 3528859579, 2646985622, 1179116369, 3174096483, 3622666293, 1094110660, 982532210, 3915875056, 3442760653, 2482674618, 3543561277, 4242258297, 1883210421, 198934262, 3881993543, 3270985024, 3814018289, 3842198594, 3180274062, 349497396, 2056365044, 3662991668, 2471767104, 2872942732, 1154111690, 3142477833, 2062459812, 3422415124, 352502659, 3206123932, 769305078, 1282348479, 3011976512, 1592394005, 976424517, 3257644548, 2159244792, 3015546726, 1321951765, 1457127034, 1008018749, 1340492242, 3250697729, 1439525819, 2116389080, 3128629141, 3912463512, 2778908372, 5179345, 2764285036, 4013718511, 76636421, 2440399146, 4124147582, 1565329027, 2314846721, 2825257189, 554997050, 2676063690, 3230428478, 4066464853, 3785792675, 3491102306, 1012514472, 710423760, 1104362914, 1402276434, 870434098, 64327618, 245834932, 4099459452, 3866904251, 2240453378, 1724463324, 1330601334, 3433676187, 829295067, 3806454686, 950099493, 4293362446, 594307004, 79190971, 2311908688, 54171305, 62487414, 3504337811, 2771970015, 1836590151, 2595431378, 3416341100, 3453307109, 1174988285, 2852396363, 346848325, 2368812712, 226406421, 3941277996, 3989222844, 3009299209, 1702732764, 2598609657, 3925497101, 331397553, 388553728, 3553027581, 2831176302, 1171547784, 2429194224, 1919275555, 2943364212, 392528745, 2077320491, 416107366, 3505919650, 2641506636, 3367202201, 2496764115, 223919825, 271108961, 2545966472, 1316212361, 3137675020, 49774935, 2744430138, 3230926645, 1183214045, 1795720081, 3453588112, 891938360, 4144344690, 2777301904, 1995233055, 3359734316, 896930090, 3330969507, 3223398016, 1321717194, 4215086939, 3506673919, 100418703, 2598322782, 1873905913, 1698737593, 1965703533, 60435064, 1751428005, 1152971074, 3618663090, 3158488445, 3727477430, 657970680, 1511931134, 1717050987, 310598970, 2234372010, 1017571582, 4084110079, 2305036871, 4254307802, 2941750258, 2165051637, 1472622743, 2543351527, 1796705211, 2214600371, 686749318, 4022876929, 2100068217, 3727699398, 3217299548, 275738892, 78573358, 2500678662, 2944914056, 1277909152, 2318080503, 3799903604, 2033312710, 1430582106, 2681053359, 427226790, 4052010686, 1405513990, 283355798, 2154582023, 3237342184, 2326232545, 3053750987, 3682467274, 4258665988, 1693455081, 3276042809, 1890575484, 3321173492, 1435919955, 372744468, 2288550928, 130181578, 464432903, 2644098717, 850876397, 366381834, 1912868480, 4114884255, 2076074274, 2025154398, 3191648339, 1180631776, 1821926123, 142706752, 3139028750, 3108622860, 1876156978, 3356317510, 3260050869, 2334989316, 747109268, 4016280193, 2897996881, 2994915453, 803723030, 1933605890, 3104516246, 533383945, 701195023, 2592103620, 1356972692, 1491149426, 4160117465, 3960597945, 2567279869, 1374045353, 3117232482, 139766291, 2589485771, 1707073928, 3210823559, 537281128, 10518971, 1901873126, 2898897661, 573642982, 760245815, 3807024923, 2334167321, 1211114995, 3530176240, 1229318785, 3602144670, 1250553934, 1010089880, 2172233573, 2688964066, 3758094780, 2941802101, 1581001398, 3746782544, 2917164021, 252667418, 1150188760, 3542252877, 1389159379, 1906599979, 3288259755, 778740684, 358910446, 26153786, 443928973, 1407665083, 298990169, 3405562703, 504530202, 3362938768, 1086122129, 3588952012, 177358838, 1668686040, 1788441005, 2920778456, 3450590302, 1707705043, 3940504028, 1650147200, 2144853533, 429939140, 2060161875, 226622212, 1271791848, 3603087696, 48155551, 966813043, 984177119, 3033759521, 3492815891, 2391190442, 3575857178, 3965974952, 459455113, 59851712, 416034666, 1727702234, 3862955095, 2038677741, 405912737, 3651584525, 1433865433, 4162114042, 319642522, 120211088, 3610217925, 1667950605, 284010502, 2536690859, 1757606927, 98163371, 1298766898, 2843598018, 2749694903, 3031345259, 2633279512, 2812045979, 34084905, 2989448216, 3311204930, 763257776, 747261640, 127287928, 326017657, 2610204813, 3746483709, 1345625337, 76875111, 1840566970, 4008707741, 1079217633, 5), None) = random<module 'random' from '/usr/local/lib/python3.12/random.py'>.getstate()86print(f" {[random<module 'random' from '/usr/local/lib/python3.12/random.py'>.randint(1, 10) for _ in range(3)]}")87random<module 'random' from '/usr/local/lib/python3.12/random.py'>.setstate(state(3, (2468570525, 44967195, 2667364560, 2449893699, 1652692239, 766678126, 273175325, 1513475390, 2407048223, 2326550691, 3055735416, 2487780036, 476975371, 81632736, 1598452444, 3338301038, 3898475993, 1749546629, 4084786842, 949316744, 2086501466, 4175211502, 3792229788, 1718685282, 2499662139, 4222931543, 3063257123, 910424605, 1400804300, 830603822, 3216023045, 2756927633, 3684278863, 3724968901, 332416530, 52016619, 2751489098, 1877715228, 1932382287, 3281876149, 3597828351, 330629843, 142483984, 1379430288, 83784318, 2266112133, 1736800492, 3746267091, 2610492607, 2079803227, 3463890091, 615297649, 2445958069, 138783768, 741209753, 3721915402, 2027708325, 4005341927, 2093884772, 119215273, 551524651, 3739622759, 3782730527, 404717681, 321534867, 1286801508, 1706479953, 2882329788, 1029701930, 2373551443, 3296995744, 468358352, 746091816, 4096927057, 641317208, 2423816852, 662051236, 1347945045, 744683282, 3532103569, 3323996770, 674188488, 2147579353, 4002509157, 1635774310, 2870381986, 1633495405, 3350196287, 225215418, 1170120648, 915993856, 814856433, 196876581, 2157558451, 3897838842, 3150173549, 626324766, 2067876245, 2163845165, 4042368565, 1376677108, 1262248675, 2205442378, 3993334766, 9743238, 2593325684, 2920379669, 1534455130, 3818766181, 931649853, 2158376649, 3577176492, 4105269980, 2743411340, 2855498512, 3468322221, 4289135738, 3070378031, 130878110, 2012459331, 3649976437, 1132601439, 747682378, 48846564, 660000069, 1790312343, 3727890972, 1155723235, 1514429407, 1230076367, 1013715474, 4196577359, 1320124222, 2614278628, 1297893158, 4083753327, 2352894470, 947894400, 2642100948, 1169889630, 1286436482, 3306394082, 3164045139, 1094362406, 809487105, 2843373296, 2280653556, 2080861721, 1562856334, 994764831, 4181417961, 1060980731, 2404272427, 3309777776, 1336994281, 634755732, 3631638369, 1391515368, 1418228798, 4257897983, 2054225289, 567832856, 1330177904, 2462727694, 814045371, 2591348022, 743574337, 2789138291, 2041853854, 894395601, 2564448893, 2991512555, 661658788, 4244382938, 592840949, 4198784705, 4208381264, 1027548464, 1699297713, 3507187687, 4228784501, 3944198753, 393010807, 1855658975, 2650303920, 837948699, 3219332495, 2923291683, 2860126530, 3856051376, 2249134764, 165767879, 2468337443, 1781864276, 2657744714, 35449830, 828146831, 117482919, 3433429317, 1819066727, 710883018, 3107854316, 3076257894, 928245986, 1936492070, 1083117887, 4108585320, 313911202, 235106869, 3091059945, 905889358, 259789608, 3447145250, 988142971, 2178196317, 859662840, 1908755715, 1247277970, 1481142601, 819671330, 2548134350, 1495134650, 4034870622, 2814194974, 2761218509, 2977430738, 614006212, 981226091, 413177493, 3471336991, 2131872665, 4009914404, 612529023, 378607496, 2988973248, 2418016553, 3050435072, 3405173865, 239315520, 553425169, 2806326921, 2194625577, 1297818883, 557367713, 1339678305, 625637250, 3007124173, 1403416408, 963253146, 557613038, 2995233521, 1599272606, 2877491804, 3025784937, 3444226192, 3778689225, 2511282536, 2036290414, 3663672933, 870613663, 3288722796, 1883286129, 2240711678, 1598432647, 1653428643, 1037288789, 3417332711, 632265342, 2992319607, 2229992519, 2627094451, 2902395192, 1798625598, 1888821172, 2928617356, 2806510607, 2169745473, 3263400237, 477483472, 2684152104, 2047416023, 1061764082, 3888197689, 3665203944, 3081648115, 1585188167, 979304208, 3283599107, 515443754, 3528859579, 2646985622, 1179116369, 3174096483, 3622666293, 1094110660, 982532210, 3915875056, 3442760653, 2482674618, 3543561277, 4242258297, 1883210421, 198934262, 3881993543, 3270985024, 3814018289, 3842198594, 3180274062, 349497396, 2056365044, 3662991668, 2471767104, 2872942732, 1154111690, 3142477833, 2062459812, 3422415124, 352502659, 3206123932, 769305078, 1282348479, 3011976512, 1592394005, 976424517, 3257644548, 2159244792, 3015546726, 1321951765, 1457127034, 1008018749, 1340492242, 3250697729, 1439525819, 2116389080, 3128629141, 3912463512, 2778908372, 5179345, 2764285036, 4013718511, 76636421, 2440399146, 4124147582, 1565329027, 2314846721, 2825257189, 554997050, 2676063690, 3230428478, 4066464853, 3785792675, 3491102306, 1012514472, 710423760, 1104362914, 1402276434, 870434098, 64327618, 245834932, 4099459452, 3866904251, 2240453378, 1724463324, 1330601334, 3433676187, 829295067, 3806454686, 950099493, 4293362446, 594307004, 79190971, 2311908688, 54171305, 62487414, 3504337811, 2771970015, 1836590151, 2595431378, 3416341100, 3453307109, 1174988285, 2852396363, 346848325, 2368812712, 226406421, 3941277996, 3989222844, 3009299209, 1702732764, 2598609657, 3925497101, 331397553, 388553728, 3553027581, 2831176302, 1171547784, 2429194224, 1919275555, 2943364212, 392528745, 2077320491, 416107366, 3505919650, 2641506636, 3367202201, 2496764115, 223919825, 271108961, 2545966472, 1316212361, 3137675020, 49774935, 2744430138, 3230926645, 1183214045, 1795720081, 3453588112, 891938360, 4144344690, 2777301904, 1995233055, 3359734316, 896930090, 3330969507, 3223398016, 1321717194, 4215086939, 3506673919, 100418703, 2598322782, 1873905913, 1698737593, 1965703533, 60435064, 1751428005, 1152971074, 3618663090, 3158488445, 3727477430, 657970680, 1511931134, 1717050987, 310598970, 2234372010, 1017571582, 4084110079, 2305036871, 4254307802, 2941750258, 2165051637, 1472622743, 2543351527, 1796705211, 2214600371, 686749318, 4022876929, 2100068217, 3727699398, 3217299548, 275738892, 78573358, 2500678662, 2944914056, 1277909152, 2318080503, 3799903604, 2033312710, 1430582106, 2681053359, 427226790, 4052010686, 1405513990, 283355798, 2154582023, 3237342184, 2326232545, 3053750987, 3682467274, 4258665988, 1693455081, 3276042809, 1890575484, 3321173492, 1435919955, 372744468, 2288550928, 130181578, 464432903, 2644098717, 850876397, 366381834, 1912868480, 4114884255, 2076074274, 2025154398, 3191648339, 1180631776, 1821926123, 142706752, 3139028750, 3108622860, 1876156978, 3356317510, 3260050869, 2334989316, 747109268, 4016280193, 2897996881, 2994915453, 803723030, 1933605890, 3104516246, 533383945, 701195023, 2592103620, 1356972692, 1491149426, 4160117465, 3960597945, 2567279869, 1374045353, 3117232482, 139766291, 2589485771, 1707073928, 3210823559, 537281128, 10518971, 1901873126, 2898897661, 573642982, 760245815, 3807024923, 2334167321, 1211114995, 3530176240, 1229318785, 3602144670, 1250553934, 1010089880, 2172233573, 2688964066, 3758094780, 2941802101, 1581001398, 3746782544, 2917164021, 252667418, 1150188760, 3542252877, 1389159379, 1906599979, 3288259755, 778740684, 358910446, 26153786, 443928973, 1407665083, 298990169, 3405562703, 504530202, 3362938768, 1086122129, 3588952012, 177358838, 1668686040, 1788441005, 2920778456, 3450590302, 1707705043, 3940504028, 1650147200, 2144853533, 429939140, 2060161875, 226622212, 1271791848, 3603087696, 48155551, 966813043, 984177119, 3033759521, 3492815891, 2391190442, 3575857178, 3965974952, 459455113, 59851712, 416034666, 1727702234, 3862955095, 2038677741, 405912737, 3651584525, 1433865433, 4162114042, 319642522, 120211088, 3610217925, 1667950605, 284010502, 2536690859, 1757606927, 98163371, 1298766898, 2843598018, 2749694903, 3031345259, 2633279512, 2812045979, 34084905, 2989448216, 3311204930, 763257776, 747261640, 127287928, 326017657, 2610204813, 3746483709, 1345625337, 76875111, 1840566970, 4008707741, 1079217633, 5), None))88print(f" {[random<module 'random' from '/usr/local/lib/python3.12/random.py'>.randint(1, 10) for _ in range(3)]}") # Same8990# Random instance (separate state)91print("\nRandom instance:")92rng→ ⟨Random A⟩ = random<module 'random' from '/usr/local/lib/python3.12/random.py'>.Random(42)93print(f" {[rng⟨Random A⟩.randint(1, 100) for _ in range(5)]}")output Seeded random (seed=42): [82, 15, 4, 95, 36] [82, 15, 4, 95, 36] Save/restore state: [4, 4, 3] [4, 4, 3] Random instance: [82, 15, 4, 95, 36]
random.seed(42)
3import random4random<module 'random' from '/usr/local/lib/python3.12/random.py'>.seed(42)56# Basic random7print("Basic random:")89# random() - [0.0, 1.0)10print("random():")11for _ in range(5):outputBasic random: random():for _ in range(5):
pass 1 of 510print("random():")11for _0 in range(5):12 print(f" {random<module 'random' from '/usr/local/lib/python3.12/random.py'>.random():.6f}")output 0.639427All 5 passes — pass 1 is the card above pass _1 0 2 1 3 2 4 3 5 4 print(" randint(1, 100):")
14# randint(a, b) - [a, b] inclusive15print("\nrandint(1, 100):")16for _ in range(10):output randint(1, 100):for _ in range(10):
pass 1 of 1015print("\nrandint(1, 100):")16for _0 in range(10):17 print(f" {random<module 'random' from '/usr/local/lib/python3.12/random.py'>.randint(1, 100)}", end=" ")18print()output 87All 10 passes — pass 1 is the card above pass _1 0 2 1 3 2 4 3 5 4 6 5 7 6 8 7 9 8 10 9 print()
17 print(f" {random.randint(1, 100)}", end=" ")18print()1920# randrange(stop) - [0, stop)21print("\nrandrange(10):")22for _ in range(10):output randrange(10):for _ in range(10):
pass 1 of 1021print("\nrandrange(10):")22for _0 in range(10):23 print(f" {random<module 'random' from '/usr/local/lib/python3.12/random.py'>.randrange(10)}", end=" ")24print()output 3All 10 passes — pass 1 is the card above pass _1 0 2 1 3 2 4 3 5 4 6 5 7 6 8 7 9 8 10 9 print()
23 print(f" {random.randrange(10)}", end=" ")24print()2526# randrange(start, stop) - [start, stop)27print("\nrandrange(10, 20):")28for _ in range(10):output randrange(10, 20):for _ in range(10):
pass 1 of 1027print("\nrandrange(10, 20):")28for _0 in range(10):29 print(f" {random<module 'random' from '/usr/local/lib/python3.12/random.py'>.randrange(10, 20)}", end=" ")30print()output 19All 10 passes — pass 1 is the card above pass _1 0 2 1 3 2 4 3 5 4 6 5 7 6 8 7 9 8 10 9 print()
29 print(f" {random.randrange(10, 20)}", end=" ")30print()3132# randrange(start, stop, step)33print("\nrandrange(0, 100, 10) - multiples of 10:")34for _ in range(10):output randrange(0, 100, 10) - multiples of 10:for _ in range(10):
pass 1 of 1033print("\nrandrange(0, 100, 10) - multiples of 10:")34for _0 in range(10):35 print(f" {random<module 'random' from '/usr/local/lib/python3.12/random.py'>.randrange(0, 100, 10)}", end=" ")36print()output 10All 10 passes — pass 1 is the card above pass _1 0 2 1 3 2 4 3 5 4 6 5 7 6 8 7 9 8 10 9 print()
35 print(f" {random.randrange(0, 100, 10)}", end=" ")36print()3738# uniform(a, b) - [a, b] or [a, b)39print("\nuniform(0.0, 10.0):")40for _ in range(5):output uniform(0.0, 10.0):for _ in range(5):
pass 1 of 539print("\nuniform(0.0, 10.0):")40for _0 in range(5):41 print(f" {random<module 'random' from '/usr/local/lib/python3.12/random.py'>.uniform(0.0, 10.0):.2f}")output 5.36All 5 passes — pass 1 is the card above pass _1 0 2 1 3 2 4 3 5 4 print(" triangular(0, 100, 50):")
43# triangular(low, high, mode)44print("\ntriangular(0, 100, 50):")45for _ in range(5):output triangular(0, 100, 50):for _ in range(5):
pass 1 of 544print("\ntriangular(0, 100, 50):")45for _0 in range(5):46 print(f" {random<module 'random' from '/usr/local/lib/python3.12/random.py'>.triangular(0, 100, 50):.2f}")output 56.33All 5 passes — pass 1 is the card above pass _1 0 2 1 3 2 4 3 5 4 colors ← ['cyan', 'magenta', 'yellow']
48# choice(seq) - random element49print("\nchoice from list:")50colors→ ['cyan', 'magenta', 'yellow'] = ['cyan', 'magenta', 'yellow']51for _ in range(10):output choice from list:for _ in range(10):
pass 1 of 1050colors = ['cyan', 'magenta', 'yellow']51for _0 in range(10):52 print(f" {random<module 'random' from '/usr/local/lib/python3.12/random.py'>.choice(colors['cyan', 'magenta', 'yellow'])}", end=" ")53print()output cyanAll 10 passes — pass 1 is the card above pass _1 0 2 1 3 2 4 3 5 4 6 5 7 6 8 7 9 8 10 9 print(f" {random.choices(colors, k=5)}")
52 print(f" {random.choice(colors)}", end=" ")53print()5455# choices(seq, k=) - with replacement56print("\nchoices (with replacement, k=5):")57print(f" {random<module 'random' from '/usr/local/lib/python3.12/random.py'>.choices(colors['cyan', 'magenta', 'yellow'], k=5)}")5859# sample(seq, k) - without replacement60print("\nsample (without replacement, k=3):")61for _ in range(3):output choices (with replacement, k=5): ['cyan', 'magenta', 'yellow', 'yellow', 'yellow'] sample (without replacement, k=3):for _ in range(3):
pass 1 of 360print("\nsample (without replacement, k=3):")61for _0 in range(3):62 print(f" {random<module 'random' from '/usr/local/lib/python3.12/random.py'>.sample(colors['cyan', 'magenta', 'yellow'], k=3)}")output ['cyan', 'yellow', 'magenta']All 3 passes — pass 1 is the card above pass _1 0 2 1 3 2 numbers ← [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
64# shuffle(list) - in-place shuffle65print("\nshuffle:")66numbers→ [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] = list(range(1, 11))67print(f"Original: {numbers[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]}")68random<module 'random' from '/usr/local/lib/python3.12/random.py'>.shuffle(numbers→ [2, 5, 8, 6, 9, 3, 7, 10, 4, 1])69print(f"Shuffled: {numbers[2, 5, 8, 6, 9, 3, 7, 10, 4, 1]}")7071# gauss(mu, sigma) - Gaussian distribution72print("\ngauss(50, 10) - mean=50, stddev=10:")73for _ in range(10):output shuffle: Original: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] Shuffled: [2, 5, 8, 6, 9, 3, 7, 10, 4, 1] gauss(50, 10) - mean=50, stddev=10:for _ in range(10):
pass 1 of 1072print("\ngauss(50, 10) - mean=50, stddev=10:")73for _0 in range(10):74 print(f" {random<module 'random' from '/usr/local/lib/python3.12/random.py'>.gauss(50, 10):.2f}")output 61.06All 10 passes — pass 1 is the card above pass _1 0 2 1 3 2 4 3 5 4 6 5 7 6 8 7 9 8 10 9 state ← (3, (2468570525, 44967195, 2667364560, 2449893699, 1652692239, 766678126, 273175325, 1513475390, 2407048223, 2326550691, 3055735416, 2487780036, 476975371, 81632736, 1598452444, 3338301038, 3898475993, 1749546629, 4084786842, 949316744, 2086501466, 4175211502, 3792229788, 1718685282, 2499662139, 4222931543, 3063257123, 910424605, 1400804300, 830603822, 3216023045, 2756927633, 3684278863, 3724968901, 332416530, 52016619, 2751489098, 1877715228, 1932382287, 3281876149, 3597828351, 330629843, 142483984, 1379430288, 83784318, 2266112133, 1736800492, 3746267091, 2610492607, 2079803227, 3463890091, 615297649, 2445958069, 138783768, 741209753, 3721915402, 2027708325, 4005341927, 2093884772, 119215273, 551524651, 3739622759, 3782730527, 404717681, 321534867, 1286801508, 1706479953, 2882329788, 1029701930, 2373551443, 3296995744, 468358352, 746091816, 4096927057, 641317208, 2423816852, 662051236, 1347945045, 744683282, 3532103569, 3323996770, 674188488, 2147579353, 4002509157, 1635774310, 2870381986, 1633495405, 3350196287, 225215418, 1170120648, 915993856, 814856433, 196876581, 2157558451, 3897838842, 3150173549, 626324766, 2067876245, 2163845165, 4042368565, 1376677108, 1262248675, 2205442378, 3993334766, 9743238, 2593325684, 2920379669, 1534455130, 3818766181, 931649853, 2158376649, 3577176492, 4105269980, 2743411340, 2855498512, 3468322221, 4289135738, 3070378031, 130878110, 2012459331, 3649976437, 1132601439, 747682378, 48846564, 660000069, 1790312343, 3727890972, 1155723235, 1514429407, 1230076367, 1013715474, 4196577359, 1320124222, 2614278628, 1297893158, 4083753327, 2352894470, 947894400, 2642100948, 1169889630, 1286436482, 3306394082, 3164045139, 1094362406, 809487105, 2843373296, 2280653556, 2080861721, 1562856334, 994764831, 4181417961, 1060980731, 2404272427, 3309777776, 1336994281, 634755732, 3631638369, 1391515368, 1418228798, 4257897983, 2054225289, 567832856, 1330177904, 2462727694, 814045371, 2591348022, 743574337, 2789138291, 2041853854, 894395601, 2564448893, 2991512555, 661658788, 4244382938, 592840949, 4198784705, 4208381264, 1027548464, 1699297713, 3507187687, 4228784501, 3944198753, 393010807, 1855658975, 2650303920, 837948699, 3219332495, 2923291683, 2860126530, 3856051376, 2249134764, 165767879, 2468337443, 1781864276, 2657744714, 35449830, 828146831, 117482919, 3433429317, 1819066727, 710883018, 3107854316, 3076257894, 928245986, 1936492070, 1083117887, 4108585320, 313911202, 235106869, 3091059945, 905889358, 259789608, 3447145250, 988142971, 2178196317, 859662840, 1908755715, 1247277970, 1481142601, 819671330, 2548134350, 1495134650, 4034870622, 2814194974, 2761218509, 2977430738, 614006212, 981226091, 413177493, 3471336991, 2131872665, 4009914404, 612529023, 378607496, 2988973248, 2418016553, 3050435072, 3405173865, 239315520, 553425169, 2806326921, 2194625577, 1297818883, 557367713, 1339678305, 625637250, 3007124173, 1403416408, 963253146, 557613038, 2995233521, 1599272606, 2877491804, 3025784937, 3444226192, 3778689225, 2511282536, 2036290414, 3663672933, 870613663, 3288722796, 1883286129, 2240711678, 1598432647, 1653428643, 1037288789, 3417332711, 632265342, 2992319607, 2229992519, 2627094451, 2902395192, 1798625598, 1888821172, 2928617356, 2806510607, 2169745473, 3263400237, 477483472, 2684152104, 2047416023, 1061764082, 3888197689, 3665203944, 3081648115, 1585188167, 979304208, 3283599107, 515443754, 3528859579, 2646985622, 1179116369, 3174096483, 3622666293, 1094110660, 982532210, 3915875056, 3442760653, 2482674618, 3543561277, 4242258297, 1883210421, 198934262, 3881993543, 3270985024, 3814018289, 3842198594, 3180274062, 349497396, 2056365044, 3662991668, 2471767104, 2872942732, 1154111690, 3142477833, 2062459812, 3422415124, 352502659, 3206123932, 769305078, 1282348479, 3011976512, 1592394005, 976424517, 3257644548, 2159244792, 3015546726, 1321951765, 1457127034, 1008018749, 1340492242, 3250697729, 1439525819, 2116389080, 3128629141, 3912463512, 2778908372, 5179345, 2764285036, 4013718511, 76636421, 2440399146, 4124147582, 1565329027, 2314846721, 2825257189, 554997050, 2676063690, 3230428478, 4066464853, 3785792675, 3491102306, 1012514472, 710423760, 1104362914, 1402276434, 870434098, 64327618, 245834932, 4099459452, 3866904251, 2240453378, 1724463324, 1330601334, 3433676187, 829295067, 3806454686, 950099493, 4293362446, 594307004, 79190971, 2311908688, 54171305, 62487414, 3504337811, 2771970015, 1836590151, 2595431378, 3416341100, 3453307109, 1174988285, 2852396363, 346848325, 2368812712, 226406421, 3941277996, 3989222844, 3009299209, 1702732764, 2598609657, 3925497101, 331397553, 388553728, 3553027581, 2831176302, 1171547784, 2429194224, 1919275555, 2943364212, 392528745, 2077320491, 416107366, 3505919650, 2641506636, 3367202201, 2496764115, 223919825, 271108961, 2545966472, 1316212361, 3137675020, 49774935, 2744430138, 3230926645, 1183214045, 1795720081, 3453588112, 891938360, 4144344690, 2777301904, 1995233055, 3359734316, 896930090, 3330969507, 3223398016, 1321717194, 4215086939, 3506673919, 100418703, 2598322782, 1873905913, 1698737593, 1965703533, 60435064, 1751428005, 1152971074, 3618663090, 3158488445, 3727477430, 657970680, 1511931134, 1717050987, 310598970, 2234372010, 1017571582, 4084110079, 2305036871, 4254307802, 2941750258, 2165051637, 1472622743, 2543351527, 1796705211, 2214600371, 686749318, 4022876929, 2100068217, 3727699398, 3217299548, 275738892, 78573358, 2500678662, 2944914056, 1277909152, 2318080503, 3799903604, 2033312710, 1430582106, 2681053359, 427226790, 4052010686, 1405513990, 283355798, 2154582023, 3237342184, 2326232545, 3053750987, 3682467274, 4258665988, 1693455081, 3276042809, 1890575484, 3321173492, 1435919955, 372744468, 2288550928, 130181578, 464432903, 2644098717, 850876397, 366381834, 1912868480, 4114884255, 2076074274, 2025154398, 3191648339, 1180631776, 1821926123, 142706752, 3139028750, 3108622860, 1876156978, 3356317510, 3260050869, 2334989316, 747109268, 4016280193, 2897996881, 2994915453, 803723030, 1933605890, 3104516246, 533383945, 701195023, 2592103620, 1356972692, 1491149426, 4160117465, 3960597945, 2567279869, 1374045353, 3117232482, 139766291, 2589485771, 1707073928, 3210823559, 537281128, 10518971, 1901873126, 2898897661, 573642982, 760245815, 3807024923, 2334167321, 1211114995, 3530176240, 1229318785, 3602144670, 1250553934, 1010089880, 2172233573, 2688964066, 3758094780, 2941802101, 1581001398, 3746782544, 2917164021, 252667418, 1150188760, 3542252877, 1389159379, 1906599979, 3288259755, 778740684, 358910446, 26153786, 443928973, 1407665083, 298990169, 3405562703, 504530202, 3362938768, 1086122129, 3588952012, 177358838, 1668686040, 1788441005, 2920778456, 3450590302, 1707705043, 3940504028, 1650147200, 2144853533, 429939140, 2060161875, 226622212, 1271791848, 3603087696, 48155551, 966813043, 984177119, 3033759521, 3492815891, 2391190442, 3575857178, 3965974952, 459455113, 59851712, 416034666, 1727702234, 3862955095, 2038677741, 405912737, 3651584525, 1433865433, 4162114042, 319642522, 120211088, 3610217925, 1667950605, 284010502, 2536690859, 1757606927, 98163371, 1298766898, 2843598018, 2749694903, 3031345259, 2633279512, 2812045979, 34084905, 2989448216, 3311204930, 763257776, 747261640, 127287928, 326017657, 2610204813, 3746483709, 1345625337, 76875111, 1840566970, 4008707741, 1079217633, 5), None)
76# seed() for reproducibility77print("\nSeeded random (seed=42):")78random<module 'random' from '/usr/local/lib/python3.12/random.py'>.seed(42)79print(f" {[random<module 'random' from '/usr/local/lib/python3.12/random.py'>.randint(1, 100) for _ in range(5)]}")80random<module 'random' from '/usr/local/lib/python3.12/random.py'>.seed(42)81print(f" {[random<module 'random' from '/usr/local/lib/python3.12/random.py'>.randint(1, 100) for _ in range(5)]}") # Same sequence8283# getstate() and setstate()84print("\nSave/restore state:")85state→ (3, (2468570525, 44967195, 2667364560, 2449893699, 1652692239, 766678126, 273175325, 1513475390, 2407048223, 2326550691, 3055735416, 2487780036, 476975371, 81632736, 1598452444, 3338301038, 3898475993, 1749546629, 4084786842, 949316744, 2086501466, 4175211502, 3792229788, 1718685282, 2499662139, 4222931543, 3063257123, 910424605, 1400804300, 830603822, 3216023045, 2756927633, 3684278863, 3724968901, 332416530, 52016619, 2751489098, 1877715228, 1932382287, 3281876149, 3597828351, 330629843, 142483984, 1379430288, 83784318, 2266112133, 1736800492, 3746267091, 2610492607, 2079803227, 3463890091, 615297649, 2445958069, 138783768, 741209753, 3721915402, 2027708325, 4005341927, 2093884772, 119215273, 551524651, 3739622759, 3782730527, 404717681, 321534867, 1286801508, 1706479953, 2882329788, 1029701930, 2373551443, 3296995744, 468358352, 746091816, 4096927057, 641317208, 2423816852, 662051236, 1347945045, 744683282, 3532103569, 3323996770, 674188488, 2147579353, 4002509157, 1635774310, 2870381986, 1633495405, 3350196287, 225215418, 1170120648, 915993856, 814856433, 196876581, 2157558451, 3897838842, 3150173549, 626324766, 2067876245, 2163845165, 4042368565, 1376677108, 1262248675, 2205442378, 3993334766, 9743238, 2593325684, 2920379669, 1534455130, 3818766181, 931649853, 2158376649, 3577176492, 4105269980, 2743411340, 2855498512, 3468322221, 4289135738, 3070378031, 130878110, 2012459331, 3649976437, 1132601439, 747682378, 48846564, 660000069, 1790312343, 3727890972, 1155723235, 1514429407, 1230076367, 1013715474, 4196577359, 1320124222, 2614278628, 1297893158, 4083753327, 2352894470, 947894400, 2642100948, 1169889630, 1286436482, 3306394082, 3164045139, 1094362406, 809487105, 2843373296, 2280653556, 2080861721, 1562856334, 994764831, 4181417961, 1060980731, 2404272427, 3309777776, 1336994281, 634755732, 3631638369, 1391515368, 1418228798, 4257897983, 2054225289, 567832856, 1330177904, 2462727694, 814045371, 2591348022, 743574337, 2789138291, 2041853854, 894395601, 2564448893, 2991512555, 661658788, 4244382938, 592840949, 4198784705, 4208381264, 1027548464, 1699297713, 3507187687, 4228784501, 3944198753, 393010807, 1855658975, 2650303920, 837948699, 3219332495, 2923291683, 2860126530, 3856051376, 2249134764, 165767879, 2468337443, 1781864276, 2657744714, 35449830, 828146831, 117482919, 3433429317, 1819066727, 710883018, 3107854316, 3076257894, 928245986, 1936492070, 1083117887, 4108585320, 313911202, 235106869, 3091059945, 905889358, 259789608, 3447145250, 988142971, 2178196317, 859662840, 1908755715, 1247277970, 1481142601, 819671330, 2548134350, 1495134650, 4034870622, 2814194974, 2761218509, 2977430738, 614006212, 981226091, 413177493, 3471336991, 2131872665, 4009914404, 612529023, 378607496, 2988973248, 2418016553, 3050435072, 3405173865, 239315520, 553425169, 2806326921, 2194625577, 1297818883, 557367713, 1339678305, 625637250, 3007124173, 1403416408, 963253146, 557613038, 2995233521, 1599272606, 2877491804, 3025784937, 3444226192, 3778689225, 2511282536, 2036290414, 3663672933, 870613663, 3288722796, 1883286129, 2240711678, 1598432647, 1653428643, 1037288789, 3417332711, 632265342, 2992319607, 2229992519, 2627094451, 2902395192, 1798625598, 1888821172, 2928617356, 2806510607, 2169745473, 3263400237, 477483472, 2684152104, 2047416023, 1061764082, 3888197689, 3665203944, 3081648115, 1585188167, 979304208, 3283599107, 515443754, 3528859579, 2646985622, 1179116369, 3174096483, 3622666293, 1094110660, 982532210, 3915875056, 3442760653, 2482674618, 3543561277, 4242258297, 1883210421, 198934262, 3881993543, 3270985024, 3814018289, 3842198594, 3180274062, 349497396, 2056365044, 3662991668, 2471767104, 2872942732, 1154111690, 3142477833, 2062459812, 3422415124, 352502659, 3206123932, 769305078, 1282348479, 3011976512, 1592394005, 976424517, 3257644548, 2159244792, 3015546726, 1321951765, 1457127034, 1008018749, 1340492242, 3250697729, 1439525819, 2116389080, 3128629141, 3912463512, 2778908372, 5179345, 2764285036, 4013718511, 76636421, 2440399146, 4124147582, 1565329027, 2314846721, 2825257189, 554997050, 2676063690, 3230428478, 4066464853, 3785792675, 3491102306, 1012514472, 710423760, 1104362914, 1402276434, 870434098, 64327618, 245834932, 4099459452, 3866904251, 2240453378, 1724463324, 1330601334, 3433676187, 829295067, 3806454686, 950099493, 4293362446, 594307004, 79190971, 2311908688, 54171305, 62487414, 3504337811, 2771970015, 1836590151, 2595431378, 3416341100, 3453307109, 1174988285, 2852396363, 346848325, 2368812712, 226406421, 3941277996, 3989222844, 3009299209, 1702732764, 2598609657, 3925497101, 331397553, 388553728, 3553027581, 2831176302, 1171547784, 2429194224, 1919275555, 2943364212, 392528745, 2077320491, 416107366, 3505919650, 2641506636, 3367202201, 2496764115, 223919825, 271108961, 2545966472, 1316212361, 3137675020, 49774935, 2744430138, 3230926645, 1183214045, 1795720081, 3453588112, 891938360, 4144344690, 2777301904, 1995233055, 3359734316, 896930090, 3330969507, 3223398016, 1321717194, 4215086939, 3506673919, 100418703, 2598322782, 1873905913, 1698737593, 1965703533, 60435064, 1751428005, 1152971074, 3618663090, 3158488445, 3727477430, 657970680, 1511931134, 1717050987, 310598970, 2234372010, 1017571582, 4084110079, 2305036871, 4254307802, 2941750258, 2165051637, 1472622743, 2543351527, 1796705211, 2214600371, 686749318, 4022876929, 2100068217, 3727699398, 3217299548, 275738892, 78573358, 2500678662, 2944914056, 1277909152, 2318080503, 3799903604, 2033312710, 1430582106, 2681053359, 427226790, 4052010686, 1405513990, 283355798, 2154582023, 3237342184, 2326232545, 3053750987, 3682467274, 4258665988, 1693455081, 3276042809, 1890575484, 3321173492, 1435919955, 372744468, 2288550928, 130181578, 464432903, 2644098717, 850876397, 366381834, 1912868480, 4114884255, 2076074274, 2025154398, 3191648339, 1180631776, 1821926123, 142706752, 3139028750, 3108622860, 1876156978, 3356317510, 3260050869, 2334989316, 747109268, 4016280193, 2897996881, 2994915453, 803723030, 1933605890, 3104516246, 533383945, 701195023, 2592103620, 1356972692, 1491149426, 4160117465, 3960597945, 2567279869, 1374045353, 3117232482, 139766291, 2589485771, 1707073928, 3210823559, 537281128, 10518971, 1901873126, 2898897661, 573642982, 760245815, 3807024923, 2334167321, 1211114995, 3530176240, 1229318785, 3602144670, 1250553934, 1010089880, 2172233573, 2688964066, 3758094780, 2941802101, 1581001398, 3746782544, 2917164021, 252667418, 1150188760, 3542252877, 1389159379, 1906599979, 3288259755, 778740684, 358910446, 26153786, 443928973, 1407665083, 298990169, 3405562703, 504530202, 3362938768, 1086122129, 3588952012, 177358838, 1668686040, 1788441005, 2920778456, 3450590302, 1707705043, 3940504028, 1650147200, 2144853533, 429939140, 2060161875, 226622212, 1271791848, 3603087696, 48155551, 966813043, 984177119, 3033759521, 3492815891, 2391190442, 3575857178, 3965974952, 459455113, 59851712, 416034666, 1727702234, 3862955095, 2038677741, 405912737, 3651584525, 1433865433, 4162114042, 319642522, 120211088, 3610217925, 1667950605, 284010502, 2536690859, 1757606927, 98163371, 1298766898, 2843598018, 2749694903, 3031345259, 2633279512, 2812045979, 34084905, 2989448216, 3311204930, 763257776, 747261640, 127287928, 326017657, 2610204813, 3746483709, 1345625337, 76875111, 1840566970, 4008707741, 1079217633, 5), None) = random<module 'random' from '/usr/local/lib/python3.12/random.py'>.getstate()86print(f" {[random<module 'random' from '/usr/local/lib/python3.12/random.py'>.randint(1, 10) for _ in range(3)]}")87random<module 'random' from '/usr/local/lib/python3.12/random.py'>.setstate(state(3, (2468570525, 44967195, 2667364560, 2449893699, 1652692239, 766678126, 273175325, 1513475390, 2407048223, 2326550691, 3055735416, 2487780036, 476975371, 81632736, 1598452444, 3338301038, 3898475993, 1749546629, 4084786842, 949316744, 2086501466, 4175211502, 3792229788, 1718685282, 2499662139, 4222931543, 3063257123, 910424605, 1400804300, 830603822, 3216023045, 2756927633, 3684278863, 3724968901, 332416530, 52016619, 2751489098, 1877715228, 1932382287, 3281876149, 3597828351, 330629843, 142483984, 1379430288, 83784318, 2266112133, 1736800492, 3746267091, 2610492607, 2079803227, 3463890091, 615297649, 2445958069, 138783768, 741209753, 3721915402, 2027708325, 4005341927, 2093884772, 119215273, 551524651, 3739622759, 3782730527, 404717681, 321534867, 1286801508, 1706479953, 2882329788, 1029701930, 2373551443, 3296995744, 468358352, 746091816, 4096927057, 641317208, 2423816852, 662051236, 1347945045, 744683282, 3532103569, 3323996770, 674188488, 2147579353, 4002509157, 1635774310, 2870381986, 1633495405, 3350196287, 225215418, 1170120648, 915993856, 814856433, 196876581, 2157558451, 3897838842, 3150173549, 626324766, 2067876245, 2163845165, 4042368565, 1376677108, 1262248675, 2205442378, 3993334766, 9743238, 2593325684, 2920379669, 1534455130, 3818766181, 931649853, 2158376649, 3577176492, 4105269980, 2743411340, 2855498512, 3468322221, 4289135738, 3070378031, 130878110, 2012459331, 3649976437, 1132601439, 747682378, 48846564, 660000069, 1790312343, 3727890972, 1155723235, 1514429407, 1230076367, 1013715474, 4196577359, 1320124222, 2614278628, 1297893158, 4083753327, 2352894470, 947894400, 2642100948, 1169889630, 1286436482, 3306394082, 3164045139, 1094362406, 809487105, 2843373296, 2280653556, 2080861721, 1562856334, 994764831, 4181417961, 1060980731, 2404272427, 3309777776, 1336994281, 634755732, 3631638369, 1391515368, 1418228798, 4257897983, 2054225289, 567832856, 1330177904, 2462727694, 814045371, 2591348022, 743574337, 2789138291, 2041853854, 894395601, 2564448893, 2991512555, 661658788, 4244382938, 592840949, 4198784705, 4208381264, 1027548464, 1699297713, 3507187687, 4228784501, 3944198753, 393010807, 1855658975, 2650303920, 837948699, 3219332495, 2923291683, 2860126530, 3856051376, 2249134764, 165767879, 2468337443, 1781864276, 2657744714, 35449830, 828146831, 117482919, 3433429317, 1819066727, 710883018, 3107854316, 3076257894, 928245986, 1936492070, 1083117887, 4108585320, 313911202, 235106869, 3091059945, 905889358, 259789608, 3447145250, 988142971, 2178196317, 859662840, 1908755715, 1247277970, 1481142601, 819671330, 2548134350, 1495134650, 4034870622, 2814194974, 2761218509, 2977430738, 614006212, 981226091, 413177493, 3471336991, 2131872665, 4009914404, 612529023, 378607496, 2988973248, 2418016553, 3050435072, 3405173865, 239315520, 553425169, 2806326921, 2194625577, 1297818883, 557367713, 1339678305, 625637250, 3007124173, 1403416408, 963253146, 557613038, 2995233521, 1599272606, 2877491804, 3025784937, 3444226192, 3778689225, 2511282536, 2036290414, 3663672933, 870613663, 3288722796, 1883286129, 2240711678, 1598432647, 1653428643, 1037288789, 3417332711, 632265342, 2992319607, 2229992519, 2627094451, 2902395192, 1798625598, 1888821172, 2928617356, 2806510607, 2169745473, 3263400237, 477483472, 2684152104, 2047416023, 1061764082, 3888197689, 3665203944, 3081648115, 1585188167, 979304208, 3283599107, 515443754, 3528859579, 2646985622, 1179116369, 3174096483, 3622666293, 1094110660, 982532210, 3915875056, 3442760653, 2482674618, 3543561277, 4242258297, 1883210421, 198934262, 3881993543, 3270985024, 3814018289, 3842198594, 3180274062, 349497396, 2056365044, 3662991668, 2471767104, 2872942732, 1154111690, 3142477833, 2062459812, 3422415124, 352502659, 3206123932, 769305078, 1282348479, 3011976512, 1592394005, 976424517, 3257644548, 2159244792, 3015546726, 1321951765, 1457127034, 1008018749, 1340492242, 3250697729, 1439525819, 2116389080, 3128629141, 3912463512, 2778908372, 5179345, 2764285036, 4013718511, 76636421, 2440399146, 4124147582, 1565329027, 2314846721, 2825257189, 554997050, 2676063690, 3230428478, 4066464853, 3785792675, 3491102306, 1012514472, 710423760, 1104362914, 1402276434, 870434098, 64327618, 245834932, 4099459452, 3866904251, 2240453378, 1724463324, 1330601334, 3433676187, 829295067, 3806454686, 950099493, 4293362446, 594307004, 79190971, 2311908688, 54171305, 62487414, 3504337811, 2771970015, 1836590151, 2595431378, 3416341100, 3453307109, 1174988285, 2852396363, 346848325, 2368812712, 226406421, 3941277996, 3989222844, 3009299209, 1702732764, 2598609657, 3925497101, 331397553, 388553728, 3553027581, 2831176302, 1171547784, 2429194224, 1919275555, 2943364212, 392528745, 2077320491, 416107366, 3505919650, 2641506636, 3367202201, 2496764115, 223919825, 271108961, 2545966472, 1316212361, 3137675020, 49774935, 2744430138, 3230926645, 1183214045, 1795720081, 3453588112, 891938360, 4144344690, 2777301904, 1995233055, 3359734316, 896930090, 3330969507, 3223398016, 1321717194, 4215086939, 3506673919, 100418703, 2598322782, 1873905913, 1698737593, 1965703533, 60435064, 1751428005, 1152971074, 3618663090, 3158488445, 3727477430, 657970680, 1511931134, 1717050987, 310598970, 2234372010, 1017571582, 4084110079, 2305036871, 4254307802, 2941750258, 2165051637, 1472622743, 2543351527, 1796705211, 2214600371, 686749318, 4022876929, 2100068217, 3727699398, 3217299548, 275738892, 78573358, 2500678662, 2944914056, 1277909152, 2318080503, 3799903604, 2033312710, 1430582106, 2681053359, 427226790, 4052010686, 1405513990, 283355798, 2154582023, 3237342184, 2326232545, 3053750987, 3682467274, 4258665988, 1693455081, 3276042809, 1890575484, 3321173492, 1435919955, 372744468, 2288550928, 130181578, 464432903, 2644098717, 850876397, 366381834, 1912868480, 4114884255, 2076074274, 2025154398, 3191648339, 1180631776, 1821926123, 142706752, 3139028750, 3108622860, 1876156978, 3356317510, 3260050869, 2334989316, 747109268, 4016280193, 2897996881, 2994915453, 803723030, 1933605890, 3104516246, 533383945, 701195023, 2592103620, 1356972692, 1491149426, 4160117465, 3960597945, 2567279869, 1374045353, 3117232482, 139766291, 2589485771, 1707073928, 3210823559, 537281128, 10518971, 1901873126, 2898897661, 573642982, 760245815, 3807024923, 2334167321, 1211114995, 3530176240, 1229318785, 3602144670, 1250553934, 1010089880, 2172233573, 2688964066, 3758094780, 2941802101, 1581001398, 3746782544, 2917164021, 252667418, 1150188760, 3542252877, 1389159379, 1906599979, 3288259755, 778740684, 358910446, 26153786, 443928973, 1407665083, 298990169, 3405562703, 504530202, 3362938768, 1086122129, 3588952012, 177358838, 1668686040, 1788441005, 2920778456, 3450590302, 1707705043, 3940504028, 1650147200, 2144853533, 429939140, 2060161875, 226622212, 1271791848, 3603087696, 48155551, 966813043, 984177119, 3033759521, 3492815891, 2391190442, 3575857178, 3965974952, 459455113, 59851712, 416034666, 1727702234, 3862955095, 2038677741, 405912737, 3651584525, 1433865433, 4162114042, 319642522, 120211088, 3610217925, 1667950605, 284010502, 2536690859, 1757606927, 98163371, 1298766898, 2843598018, 2749694903, 3031345259, 2633279512, 2812045979, 34084905, 2989448216, 3311204930, 763257776, 747261640, 127287928, 326017657, 2610204813, 3746483709, 1345625337, 76875111, 1840566970, 4008707741, 1079217633, 5), None))88print(f" {[random<module 'random' from '/usr/local/lib/python3.12/random.py'>.randint(1, 10) for _ in range(3)]}") # Same8990# Random instance (separate state)91print("\nRandom instance:")92rng→ ⟨Random A⟩ = random<module 'random' from '/usr/local/lib/python3.12/random.py'>.Random(42)93print(f" {[rng⟨Random A⟩.randint(1, 100) for _ in range(5)]}")output Seeded random (seed=42): [82, 15, 4, 95, 36] [82, 15, 4, 95, 36] Save/restore state: [4, 4, 3] [4, 4, 3] Random instance: [82, 15, 4, 95, 36]
random.seed(42)
3import random4random<module 'random' from '/usr/local/lib/python3.12/random.py'>.seed(42)56# Basic random7print("Basic random:")89# random() - [0.0, 1.0)10print("random():")11for _ in range(5):outputBasic random: random():for _ in range(5):
pass 1 of 510print("random():")11for _0 in range(5):12 print(f" {random<module 'random' from '/usr/local/lib/python3.12/random.py'>.random():.6f}")output 0.639427All 5 passes — pass 1 is the card above pass _1 0 2 1 3 2 4 3 5 4 print(" randint(1, 100):")
14# randint(a, b) - [a, b] inclusive15print("\nrandint(1, 100):")16for _ in range(10):output randint(1, 100):for _ in range(10):
pass 1 of 1015print("\nrandint(1, 100):")16for _0 in range(10):17 print(f" {random<module 'random' from '/usr/local/lib/python3.12/random.py'>.randint(1, 100)}", end=" ")18print()output 87All 10 passes — pass 1 is the card above pass _1 0 2 1 3 2 4 3 5 4 6 5 7 6 8 7 9 8 10 9 print()
17 print(f" {random.randint(1, 100)}", end=" ")18print()1920# randrange(stop) - [0, stop)21print("\nrandrange(10):")22for _ in range(10):output randrange(10):for _ in range(10):
pass 1 of 1021print("\nrandrange(10):")22for _0 in range(10):23 print(f" {random<module 'random' from '/usr/local/lib/python3.12/random.py'>.randrange(10)}", end=" ")24print()output 3All 10 passes — pass 1 is the card above pass _1 0 2 1 3 2 4 3 5 4 6 5 7 6 8 7 9 8 10 9 print()
23 print(f" {random.randrange(10)}", end=" ")24print()2526# randrange(start, stop) - [start, stop)27print("\nrandrange(10, 20):")28for _ in range(10):output randrange(10, 20):for _ in range(10):
pass 1 of 1027print("\nrandrange(10, 20):")28for _0 in range(10):29 print(f" {random<module 'random' from '/usr/local/lib/python3.12/random.py'>.randrange(10, 20)}", end=" ")30print()output 19All 10 passes — pass 1 is the card above pass _1 0 2 1 3 2 4 3 5 4 6 5 7 6 8 7 9 8 10 9 print()
29 print(f" {random.randrange(10, 20)}", end=" ")30print()3132# randrange(start, stop, step)33print("\nrandrange(0, 100, 10) - multiples of 10:")34for _ in range(10):output randrange(0, 100, 10) - multiples of 10:for _ in range(10):
pass 1 of 1033print("\nrandrange(0, 100, 10) - multiples of 10:")34for _0 in range(10):35 print(f" {random<module 'random' from '/usr/local/lib/python3.12/random.py'>.randrange(0, 100, 10)}", end=" ")36print()output 10All 10 passes — pass 1 is the card above pass _1 0 2 1 3 2 4 3 5 4 6 5 7 6 8 7 9 8 10 9 print()
35 print(f" {random.randrange(0, 100, 10)}", end=" ")36print()3738# uniform(a, b) - [a, b] or [a, b)39print("\nuniform(0.0, 10.0):")40for _ in range(5):output uniform(0.0, 10.0):for _ in range(5):
pass 1 of 539print("\nuniform(0.0, 10.0):")40for _0 in range(5):41 print(f" {random<module 'random' from '/usr/local/lib/python3.12/random.py'>.uniform(0.0, 10.0):.2f}")output 5.36All 5 passes — pass 1 is the card above pass _1 0 2 1 3 2 4 3 5 4 print(" triangular(0, 100, 50):")
43# triangular(low, high, mode)44print("\ntriangular(0, 100, 50):")45for _ in range(5):output triangular(0, 100, 50):for _ in range(5):
pass 1 of 544print("\ntriangular(0, 100, 50):")45for _0 in range(5):46 print(f" {random<module 'random' from '/usr/local/lib/python3.12/random.py'>.triangular(0, 100, 50):.2f}")output 56.33All 5 passes — pass 1 is the card above pass _1 0 2 1 3 2 4 3 5 4 colors ← ['red', 'green', 'blue']
48# choice(seq) - random element49print("\nchoice from list:")50colors→ ['red', 'green', 'blue'] = ['red', 'green', 'blue']51for _ in range(10):output choice from list:for _ in range(10):
pass 1 of 1050colors = ['red', 'green', 'blue']51for _0 in range(10):52 print(f" {random<module 'random' from '/usr/local/lib/python3.12/random.py'>.choice(colors['red', 'green', 'blue'])}", end=" ")53print()output redAll 10 passes — pass 1 is the card above pass _1 0 2 1 3 2 4 3 5 4 6 5 7 6 8 7 9 8 10 9 print(f" {random.choices(colors, k=5)}")
52 print(f" {random.choice(colors)}", end=" ")53print()5455# choices(seq, k=) - with replacement56print("\nchoices (with replacement, k=5):")57print(f" {random<module 'random' from '/usr/local/lib/python3.12/random.py'>.choices(colors['red', 'green', 'blue'], k=5)}")5859# sample(seq, k) - without replacement60print("\nsample (without replacement, k=3):")61for _ in range(3):output choices (with replacement, k=5): ['red', 'green', 'blue', 'blue', 'blue'] sample (without replacement, k=3):for _ in range(3):
pass 1 of 360print("\nsample (without replacement, k=3):")61for _0 in range(3):62 print(f" {random<module 'random' from '/usr/local/lib/python3.12/random.py'>.sample(colors['red', 'green', 'blue'], k=3)}")output ['red', 'blue', 'green']All 3 passes — pass 1 is the card above pass _1 0 2 1 3 2 numbers ← [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
64# shuffle(list) - in-place shuffle65print("\nshuffle:")66numbers→ [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] = list(range(1, 11))67print(f"Original: {numbers[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]}")68random<module 'random' from '/usr/local/lib/python3.12/random.py'>.shuffle(numbers→ [2, 5, 8, 6, 9, 3, 7, 10, 4, 1])69print(f"Shuffled: {numbers[2, 5, 8, 6, 9, 3, 7, 10, 4, 1]}")7071# gauss(mu, sigma) - Gaussian distribution72print("\ngauss(50, 10) - mean=50, stddev=10:")73for _ in range(10):output shuffle: Original: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] Shuffled: [2, 5, 8, 6, 9, 3, 7, 10, 4, 1] gauss(50, 10) - mean=50, stddev=10:for _ in range(10):
pass 1 of 1072print("\ngauss(50, 10) - mean=50, stddev=10:")73for _0 in range(10):74 print(f" {random<module 'random' from '/usr/local/lib/python3.12/random.py'>.gauss(50, 10):.2f}")output 61.06All 10 passes — pass 1 is the card above pass _1 0 2 1 3 2 4 3 5 4 6 5 7 6 8 7 9 8 10 9 state ← (3, (2468570525, 44967195, 2667364560, 2449893699, 1652692239, 766678126, 273175325, 1513475390, 2407048223, 2326550691, 3055735416, 2487780036, 476975371, 81632736, 1598452444, 3338301038, 3898475993, 1749546629, 4084786842, 949316744, 2086501466, 4175211502, 3792229788, 1718685282, 2499662139, 4222931543, 3063257123, 910424605, 1400804300, 830603822, 3216023045, 2756927633, 3684278863, 3724968901, 332416530, 52016619, 2751489098, 1877715228, 1932382287, 3281876149, 3597828351, 330629843, 142483984, 1379430288, 83784318, 2266112133, 1736800492, 3746267091, 2610492607, 2079803227, 3463890091, 615297649, 2445958069, 138783768, 741209753, 3721915402, 2027708325, 4005341927, 2093884772, 119215273, 551524651, 3739622759, 3782730527, 404717681, 321534867, 1286801508, 1706479953, 2882329788, 1029701930, 2373551443, 3296995744, 468358352, 746091816, 4096927057, 641317208, 2423816852, 662051236, 1347945045, 744683282, 3532103569, 3323996770, 674188488, 2147579353, 4002509157, 1635774310, 2870381986, 1633495405, 3350196287, 225215418, 1170120648, 915993856, 814856433, 196876581, 2157558451, 3897838842, 3150173549, 626324766, 2067876245, 2163845165, 4042368565, 1376677108, 1262248675, 2205442378, 3993334766, 9743238, 2593325684, 2920379669, 1534455130, 3818766181, 931649853, 2158376649, 3577176492, 4105269980, 2743411340, 2855498512, 3468322221, 4289135738, 3070378031, 130878110, 2012459331, 3649976437, 1132601439, 747682378, 48846564, 660000069, 1790312343, 3727890972, 1155723235, 1514429407, 1230076367, 1013715474, 4196577359, 1320124222, 2614278628, 1297893158, 4083753327, 2352894470, 947894400, 2642100948, 1169889630, 1286436482, 3306394082, 3164045139, 1094362406, 809487105, 2843373296, 2280653556, 2080861721, 1562856334, 994764831, 4181417961, 1060980731, 2404272427, 3309777776, 1336994281, 634755732, 3631638369, 1391515368, 1418228798, 4257897983, 2054225289, 567832856, 1330177904, 2462727694, 814045371, 2591348022, 743574337, 2789138291, 2041853854, 894395601, 2564448893, 2991512555, 661658788, 4244382938, 592840949, 4198784705, 4208381264, 1027548464, 1699297713, 3507187687, 4228784501, 3944198753, 393010807, 1855658975, 2650303920, 837948699, 3219332495, 2923291683, 2860126530, 3856051376, 2249134764, 165767879, 2468337443, 1781864276, 2657744714, 35449830, 828146831, 117482919, 3433429317, 1819066727, 710883018, 3107854316, 3076257894, 928245986, 1936492070, 1083117887, 4108585320, 313911202, 235106869, 3091059945, 905889358, 259789608, 3447145250, 988142971, 2178196317, 859662840, 1908755715, 1247277970, 1481142601, 819671330, 2548134350, 1495134650, 4034870622, 2814194974, 2761218509, 2977430738, 614006212, 981226091, 413177493, 3471336991, 2131872665, 4009914404, 612529023, 378607496, 2988973248, 2418016553, 3050435072, 3405173865, 239315520, 553425169, 2806326921, 2194625577, 1297818883, 557367713, 1339678305, 625637250, 3007124173, 1403416408, 963253146, 557613038, 2995233521, 1599272606, 2877491804, 3025784937, 3444226192, 3778689225, 2511282536, 2036290414, 3663672933, 870613663, 3288722796, 1883286129, 2240711678, 1598432647, 1653428643, 1037288789, 3417332711, 632265342, 2992319607, 2229992519, 2627094451, 2902395192, 1798625598, 1888821172, 2928617356, 2806510607, 2169745473, 3263400237, 477483472, 2684152104, 2047416023, 1061764082, 3888197689, 3665203944, 3081648115, 1585188167, 979304208, 3283599107, 515443754, 3528859579, 2646985622, 1179116369, 3174096483, 3622666293, 1094110660, 982532210, 3915875056, 3442760653, 2482674618, 3543561277, 4242258297, 1883210421, 198934262, 3881993543, 3270985024, 3814018289, 3842198594, 3180274062, 349497396, 2056365044, 3662991668, 2471767104, 2872942732, 1154111690, 3142477833, 2062459812, 3422415124, 352502659, 3206123932, 769305078, 1282348479, 3011976512, 1592394005, 976424517, 3257644548, 2159244792, 3015546726, 1321951765, 1457127034, 1008018749, 1340492242, 3250697729, 1439525819, 2116389080, 3128629141, 3912463512, 2778908372, 5179345, 2764285036, 4013718511, 76636421, 2440399146, 4124147582, 1565329027, 2314846721, 2825257189, 554997050, 2676063690, 3230428478, 4066464853, 3785792675, 3491102306, 1012514472, 710423760, 1104362914, 1402276434, 870434098, 64327618, 245834932, 4099459452, 3866904251, 2240453378, 1724463324, 1330601334, 3433676187, 829295067, 3806454686, 950099493, 4293362446, 594307004, 79190971, 2311908688, 54171305, 62487414, 3504337811, 2771970015, 1836590151, 2595431378, 3416341100, 3453307109, 1174988285, 2852396363, 346848325, 2368812712, 226406421, 3941277996, 3989222844, 3009299209, 1702732764, 2598609657, 3925497101, 331397553, 388553728, 3553027581, 2831176302, 1171547784, 2429194224, 1919275555, 2943364212, 392528745, 2077320491, 416107366, 3505919650, 2641506636, 3367202201, 2496764115, 223919825, 271108961, 2545966472, 1316212361, 3137675020, 49774935, 2744430138, 3230926645, 1183214045, 1795720081, 3453588112, 891938360, 4144344690, 2777301904, 1995233055, 3359734316, 896930090, 3330969507, 3223398016, 1321717194, 4215086939, 3506673919, 100418703, 2598322782, 1873905913, 1698737593, 1965703533, 60435064, 1751428005, 1152971074, 3618663090, 3158488445, 3727477430, 657970680, 1511931134, 1717050987, 310598970, 2234372010, 1017571582, 4084110079, 2305036871, 4254307802, 2941750258, 2165051637, 1472622743, 2543351527, 1796705211, 2214600371, 686749318, 4022876929, 2100068217, 3727699398, 3217299548, 275738892, 78573358, 2500678662, 2944914056, 1277909152, 2318080503, 3799903604, 2033312710, 1430582106, 2681053359, 427226790, 4052010686, 1405513990, 283355798, 2154582023, 3237342184, 2326232545, 3053750987, 3682467274, 4258665988, 1693455081, 3276042809, 1890575484, 3321173492, 1435919955, 372744468, 2288550928, 130181578, 464432903, 2644098717, 850876397, 366381834, 1912868480, 4114884255, 2076074274, 2025154398, 3191648339, 1180631776, 1821926123, 142706752, 3139028750, 3108622860, 1876156978, 3356317510, 3260050869, 2334989316, 747109268, 4016280193, 2897996881, 2994915453, 803723030, 1933605890, 3104516246, 533383945, 701195023, 2592103620, 1356972692, 1491149426, 4160117465, 3960597945, 2567279869, 1374045353, 3117232482, 139766291, 2589485771, 1707073928, 3210823559, 537281128, 10518971, 1901873126, 2898897661, 573642982, 760245815, 3807024923, 2334167321, 1211114995, 3530176240, 1229318785, 3602144670, 1250553934, 1010089880, 2172233573, 2688964066, 3758094780, 2941802101, 1581001398, 3746782544, 2917164021, 252667418, 1150188760, 3542252877, 1389159379, 1906599979, 3288259755, 778740684, 358910446, 26153786, 443928973, 1407665083, 298990169, 3405562703, 504530202, 3362938768, 1086122129, 3588952012, 177358838, 1668686040, 1788441005, 2920778456, 3450590302, 1707705043, 3940504028, 1650147200, 2144853533, 429939140, 2060161875, 226622212, 1271791848, 3603087696, 48155551, 966813043, 984177119, 3033759521, 3492815891, 2391190442, 3575857178, 3965974952, 459455113, 59851712, 416034666, 1727702234, 3862955095, 2038677741, 405912737, 3651584525, 1433865433, 4162114042, 319642522, 120211088, 3610217925, 1667950605, 284010502, 2536690859, 1757606927, 98163371, 1298766898, 2843598018, 2749694903, 3031345259, 2633279512, 2812045979, 34084905, 2989448216, 3311204930, 763257776, 747261640, 127287928, 326017657, 2610204813, 3746483709, 1345625337, 76875111, 1840566970, 4008707741, 1079217633, 5), None)
76# seed() for reproducibility77print("\nSeeded random (seed=42):")78random<module 'random' from '/usr/local/lib/python3.12/random.py'>.seed(42)79print(f" {[random<module 'random' from '/usr/local/lib/python3.12/random.py'>.randint(1, 100) for _ in range(5)]}")80random<module 'random' from '/usr/local/lib/python3.12/random.py'>.seed(42)81print(f" {[random<module 'random' from '/usr/local/lib/python3.12/random.py'>.randint(1, 100) for _ in range(5)]}") # Same sequence8283# getstate() and setstate()84print("\nSave/restore state:")85state→ (3, (2468570525, 44967195, 2667364560, 2449893699, 1652692239, 766678126, 273175325, 1513475390, 2407048223, 2326550691, 3055735416, 2487780036, 476975371, 81632736, 1598452444, 3338301038, 3898475993, 1749546629, 4084786842, 949316744, 2086501466, 4175211502, 3792229788, 1718685282, 2499662139, 4222931543, 3063257123, 910424605, 1400804300, 830603822, 3216023045, 2756927633, 3684278863, 3724968901, 332416530, 52016619, 2751489098, 1877715228, 1932382287, 3281876149, 3597828351, 330629843, 142483984, 1379430288, 83784318, 2266112133, 1736800492, 3746267091, 2610492607, 2079803227, 3463890091, 615297649, 2445958069, 138783768, 741209753, 3721915402, 2027708325, 4005341927, 2093884772, 119215273, 551524651, 3739622759, 3782730527, 404717681, 321534867, 1286801508, 1706479953, 2882329788, 1029701930, 2373551443, 3296995744, 468358352, 746091816, 4096927057, 641317208, 2423816852, 662051236, 1347945045, 744683282, 3532103569, 3323996770, 674188488, 2147579353, 4002509157, 1635774310, 2870381986, 1633495405, 3350196287, 225215418, 1170120648, 915993856, 814856433, 196876581, 2157558451, 3897838842, 3150173549, 626324766, 2067876245, 2163845165, 4042368565, 1376677108, 1262248675, 2205442378, 3993334766, 9743238, 2593325684, 2920379669, 1534455130, 3818766181, 931649853, 2158376649, 3577176492, 4105269980, 2743411340, 2855498512, 3468322221, 4289135738, 3070378031, 130878110, 2012459331, 3649976437, 1132601439, 747682378, 48846564, 660000069, 1790312343, 3727890972, 1155723235, 1514429407, 1230076367, 1013715474, 4196577359, 1320124222, 2614278628, 1297893158, 4083753327, 2352894470, 947894400, 2642100948, 1169889630, 1286436482, 3306394082, 3164045139, 1094362406, 809487105, 2843373296, 2280653556, 2080861721, 1562856334, 994764831, 4181417961, 1060980731, 2404272427, 3309777776, 1336994281, 634755732, 3631638369, 1391515368, 1418228798, 4257897983, 2054225289, 567832856, 1330177904, 2462727694, 814045371, 2591348022, 743574337, 2789138291, 2041853854, 894395601, 2564448893, 2991512555, 661658788, 4244382938, 592840949, 4198784705, 4208381264, 1027548464, 1699297713, 3507187687, 4228784501, 3944198753, 393010807, 1855658975, 2650303920, 837948699, 3219332495, 2923291683, 2860126530, 3856051376, 2249134764, 165767879, 2468337443, 1781864276, 2657744714, 35449830, 828146831, 117482919, 3433429317, 1819066727, 710883018, 3107854316, 3076257894, 928245986, 1936492070, 1083117887, 4108585320, 313911202, 235106869, 3091059945, 905889358, 259789608, 3447145250, 988142971, 2178196317, 859662840, 1908755715, 1247277970, 1481142601, 819671330, 2548134350, 1495134650, 4034870622, 2814194974, 2761218509, 2977430738, 614006212, 981226091, 413177493, 3471336991, 2131872665, 4009914404, 612529023, 378607496, 2988973248, 2418016553, 3050435072, 3405173865, 239315520, 553425169, 2806326921, 2194625577, 1297818883, 557367713, 1339678305, 625637250, 3007124173, 1403416408, 963253146, 557613038, 2995233521, 1599272606, 2877491804, 3025784937, 3444226192, 3778689225, 2511282536, 2036290414, 3663672933, 870613663, 3288722796, 1883286129, 2240711678, 1598432647, 1653428643, 1037288789, 3417332711, 632265342, 2992319607, 2229992519, 2627094451, 2902395192, 1798625598, 1888821172, 2928617356, 2806510607, 2169745473, 3263400237, 477483472, 2684152104, 2047416023, 1061764082, 3888197689, 3665203944, 3081648115, 1585188167, 979304208, 3283599107, 515443754, 3528859579, 2646985622, 1179116369, 3174096483, 3622666293, 1094110660, 982532210, 3915875056, 3442760653, 2482674618, 3543561277, 4242258297, 1883210421, 198934262, 3881993543, 3270985024, 3814018289, 3842198594, 3180274062, 349497396, 2056365044, 3662991668, 2471767104, 2872942732, 1154111690, 3142477833, 2062459812, 3422415124, 352502659, 3206123932, 769305078, 1282348479, 3011976512, 1592394005, 976424517, 3257644548, 2159244792, 3015546726, 1321951765, 1457127034, 1008018749, 1340492242, 3250697729, 1439525819, 2116389080, 3128629141, 3912463512, 2778908372, 5179345, 2764285036, 4013718511, 76636421, 2440399146, 4124147582, 1565329027, 2314846721, 2825257189, 554997050, 2676063690, 3230428478, 4066464853, 3785792675, 3491102306, 1012514472, 710423760, 1104362914, 1402276434, 870434098, 64327618, 245834932, 4099459452, 3866904251, 2240453378, 1724463324, 1330601334, 3433676187, 829295067, 3806454686, 950099493, 4293362446, 594307004, 79190971, 2311908688, 54171305, 62487414, 3504337811, 2771970015, 1836590151, 2595431378, 3416341100, 3453307109, 1174988285, 2852396363, 346848325, 2368812712, 226406421, 3941277996, 3989222844, 3009299209, 1702732764, 2598609657, 3925497101, 331397553, 388553728, 3553027581, 2831176302, 1171547784, 2429194224, 1919275555, 2943364212, 392528745, 2077320491, 416107366, 3505919650, 2641506636, 3367202201, 2496764115, 223919825, 271108961, 2545966472, 1316212361, 3137675020, 49774935, 2744430138, 3230926645, 1183214045, 1795720081, 3453588112, 891938360, 4144344690, 2777301904, 1995233055, 3359734316, 896930090, 3330969507, 3223398016, 1321717194, 4215086939, 3506673919, 100418703, 2598322782, 1873905913, 1698737593, 1965703533, 60435064, 1751428005, 1152971074, 3618663090, 3158488445, 3727477430, 657970680, 1511931134, 1717050987, 310598970, 2234372010, 1017571582, 4084110079, 2305036871, 4254307802, 2941750258, 2165051637, 1472622743, 2543351527, 1796705211, 2214600371, 686749318, 4022876929, 2100068217, 3727699398, 3217299548, 275738892, 78573358, 2500678662, 2944914056, 1277909152, 2318080503, 3799903604, 2033312710, 1430582106, 2681053359, 427226790, 4052010686, 1405513990, 283355798, 2154582023, 3237342184, 2326232545, 3053750987, 3682467274, 4258665988, 1693455081, 3276042809, 1890575484, 3321173492, 1435919955, 372744468, 2288550928, 130181578, 464432903, 2644098717, 850876397, 366381834, 1912868480, 4114884255, 2076074274, 2025154398, 3191648339, 1180631776, 1821926123, 142706752, 3139028750, 3108622860, 1876156978, 3356317510, 3260050869, 2334989316, 747109268, 4016280193, 2897996881, 2994915453, 803723030, 1933605890, 3104516246, 533383945, 701195023, 2592103620, 1356972692, 1491149426, 4160117465, 3960597945, 2567279869, 1374045353, 3117232482, 139766291, 2589485771, 1707073928, 3210823559, 537281128, 10518971, 1901873126, 2898897661, 573642982, 760245815, 3807024923, 2334167321, 1211114995, 3530176240, 1229318785, 3602144670, 1250553934, 1010089880, 2172233573, 2688964066, 3758094780, 2941802101, 1581001398, 3746782544, 2917164021, 252667418, 1150188760, 3542252877, 1389159379, 1906599979, 3288259755, 778740684, 358910446, 26153786, 443928973, 1407665083, 298990169, 3405562703, 504530202, 3362938768, 1086122129, 3588952012, 177358838, 1668686040, 1788441005, 2920778456, 3450590302, 1707705043, 3940504028, 1650147200, 2144853533, 429939140, 2060161875, 226622212, 1271791848, 3603087696, 48155551, 966813043, 984177119, 3033759521, 3492815891, 2391190442, 3575857178, 3965974952, 459455113, 59851712, 416034666, 1727702234, 3862955095, 2038677741, 405912737, 3651584525, 1433865433, 4162114042, 319642522, 120211088, 3610217925, 1667950605, 284010502, 2536690859, 1757606927, 98163371, 1298766898, 2843598018, 2749694903, 3031345259, 2633279512, 2812045979, 34084905, 2989448216, 3311204930, 763257776, 747261640, 127287928, 326017657, 2610204813, 3746483709, 1345625337, 76875111, 1840566970, 4008707741, 1079217633, 5), None) = random<module 'random' from '/usr/local/lib/python3.12/random.py'>.getstate()86print(f" {[random<module 'random' from '/usr/local/lib/python3.12/random.py'>.randint(1, 10) for _ in range(3)]}")87random<module 'random' from '/usr/local/lib/python3.12/random.py'>.setstate(state(3, (2468570525, 44967195, 2667364560, 2449893699, 1652692239, 766678126, 273175325, 1513475390, 2407048223, 2326550691, 3055735416, 2487780036, 476975371, 81632736, 1598452444, 3338301038, 3898475993, 1749546629, 4084786842, 949316744, 2086501466, 4175211502, 3792229788, 1718685282, 2499662139, 4222931543, 3063257123, 910424605, 1400804300, 830603822, 3216023045, 2756927633, 3684278863, 3724968901, 332416530, 52016619, 2751489098, 1877715228, 1932382287, 3281876149, 3597828351, 330629843, 142483984, 1379430288, 83784318, 2266112133, 1736800492, 3746267091, 2610492607, 2079803227, 3463890091, 615297649, 2445958069, 138783768, 741209753, 3721915402, 2027708325, 4005341927, 2093884772, 119215273, 551524651, 3739622759, 3782730527, 404717681, 321534867, 1286801508, 1706479953, 2882329788, 1029701930, 2373551443, 3296995744, 468358352, 746091816, 4096927057, 641317208, 2423816852, 662051236, 1347945045, 744683282, 3532103569, 3323996770, 674188488, 2147579353, 4002509157, 1635774310, 2870381986, 1633495405, 3350196287, 225215418, 1170120648, 915993856, 814856433, 196876581, 2157558451, 3897838842, 3150173549, 626324766, 2067876245, 2163845165, 4042368565, 1376677108, 1262248675, 2205442378, 3993334766, 9743238, 2593325684, 2920379669, 1534455130, 3818766181, 931649853, 2158376649, 3577176492, 4105269980, 2743411340, 2855498512, 3468322221, 4289135738, 3070378031, 130878110, 2012459331, 3649976437, 1132601439, 747682378, 48846564, 660000069, 1790312343, 3727890972, 1155723235, 1514429407, 1230076367, 1013715474, 4196577359, 1320124222, 2614278628, 1297893158, 4083753327, 2352894470, 947894400, 2642100948, 1169889630, 1286436482, 3306394082, 3164045139, 1094362406, 809487105, 2843373296, 2280653556, 2080861721, 1562856334, 994764831, 4181417961, 1060980731, 2404272427, 3309777776, 1336994281, 634755732, 3631638369, 1391515368, 1418228798, 4257897983, 2054225289, 567832856, 1330177904, 2462727694, 814045371, 2591348022, 743574337, 2789138291, 2041853854, 894395601, 2564448893, 2991512555, 661658788, 4244382938, 592840949, 4198784705, 4208381264, 1027548464, 1699297713, 3507187687, 4228784501, 3944198753, 393010807, 1855658975, 2650303920, 837948699, 3219332495, 2923291683, 2860126530, 3856051376, 2249134764, 165767879, 2468337443, 1781864276, 2657744714, 35449830, 828146831, 117482919, 3433429317, 1819066727, 710883018, 3107854316, 3076257894, 928245986, 1936492070, 1083117887, 4108585320, 313911202, 235106869, 3091059945, 905889358, 259789608, 3447145250, 988142971, 2178196317, 859662840, 1908755715, 1247277970, 1481142601, 819671330, 2548134350, 1495134650, 4034870622, 2814194974, 2761218509, 2977430738, 614006212, 981226091, 413177493, 3471336991, 2131872665, 4009914404, 612529023, 378607496, 2988973248, 2418016553, 3050435072, 3405173865, 239315520, 553425169, 2806326921, 2194625577, 1297818883, 557367713, 1339678305, 625637250, 3007124173, 1403416408, 963253146, 557613038, 2995233521, 1599272606, 2877491804, 3025784937, 3444226192, 3778689225, 2511282536, 2036290414, 3663672933, 870613663, 3288722796, 1883286129, 2240711678, 1598432647, 1653428643, 1037288789, 3417332711, 632265342, 2992319607, 2229992519, 2627094451, 2902395192, 1798625598, 1888821172, 2928617356, 2806510607, 2169745473, 3263400237, 477483472, 2684152104, 2047416023, 1061764082, 3888197689, 3665203944, 3081648115, 1585188167, 979304208, 3283599107, 515443754, 3528859579, 2646985622, 1179116369, 3174096483, 3622666293, 1094110660, 982532210, 3915875056, 3442760653, 2482674618, 3543561277, 4242258297, 1883210421, 198934262, 3881993543, 3270985024, 3814018289, 3842198594, 3180274062, 349497396, 2056365044, 3662991668, 2471767104, 2872942732, 1154111690, 3142477833, 2062459812, 3422415124, 352502659, 3206123932, 769305078, 1282348479, 3011976512, 1592394005, 976424517, 3257644548, 2159244792, 3015546726, 1321951765, 1457127034, 1008018749, 1340492242, 3250697729, 1439525819, 2116389080, 3128629141, 3912463512, 2778908372, 5179345, 2764285036, 4013718511, 76636421, 2440399146, 4124147582, 1565329027, 2314846721, 2825257189, 554997050, 2676063690, 3230428478, 4066464853, 3785792675, 3491102306, 1012514472, 710423760, 1104362914, 1402276434, 870434098, 64327618, 245834932, 4099459452, 3866904251, 2240453378, 1724463324, 1330601334, 3433676187, 829295067, 3806454686, 950099493, 4293362446, 594307004, 79190971, 2311908688, 54171305, 62487414, 3504337811, 2771970015, 1836590151, 2595431378, 3416341100, 3453307109, 1174988285, 2852396363, 346848325, 2368812712, 226406421, 3941277996, 3989222844, 3009299209, 1702732764, 2598609657, 3925497101, 331397553, 388553728, 3553027581, 2831176302, 1171547784, 2429194224, 1919275555, 2943364212, 392528745, 2077320491, 416107366, 3505919650, 2641506636, 3367202201, 2496764115, 223919825, 271108961, 2545966472, 1316212361, 3137675020, 49774935, 2744430138, 3230926645, 1183214045, 1795720081, 3453588112, 891938360, 4144344690, 2777301904, 1995233055, 3359734316, 896930090, 3330969507, 3223398016, 1321717194, 4215086939, 3506673919, 100418703, 2598322782, 1873905913, 1698737593, 1965703533, 60435064, 1751428005, 1152971074, 3618663090, 3158488445, 3727477430, 657970680, 1511931134, 1717050987, 310598970, 2234372010, 1017571582, 4084110079, 2305036871, 4254307802, 2941750258, 2165051637, 1472622743, 2543351527, 1796705211, 2214600371, 686749318, 4022876929, 2100068217, 3727699398, 3217299548, 275738892, 78573358, 2500678662, 2944914056, 1277909152, 2318080503, 3799903604, 2033312710, 1430582106, 2681053359, 427226790, 4052010686, 1405513990, 283355798, 2154582023, 3237342184, 2326232545, 3053750987, 3682467274, 4258665988, 1693455081, 3276042809, 1890575484, 3321173492, 1435919955, 372744468, 2288550928, 130181578, 464432903, 2644098717, 850876397, 366381834, 1912868480, 4114884255, 2076074274, 2025154398, 3191648339, 1180631776, 1821926123, 142706752, 3139028750, 3108622860, 1876156978, 3356317510, 3260050869, 2334989316, 747109268, 4016280193, 2897996881, 2994915453, 803723030, 1933605890, 3104516246, 533383945, 701195023, 2592103620, 1356972692, 1491149426, 4160117465, 3960597945, 2567279869, 1374045353, 3117232482, 139766291, 2589485771, 1707073928, 3210823559, 537281128, 10518971, 1901873126, 2898897661, 573642982, 760245815, 3807024923, 2334167321, 1211114995, 3530176240, 1229318785, 3602144670, 1250553934, 1010089880, 2172233573, 2688964066, 3758094780, 2941802101, 1581001398, 3746782544, 2917164021, 252667418, 1150188760, 3542252877, 1389159379, 1906599979, 3288259755, 778740684, 358910446, 26153786, 443928973, 1407665083, 298990169, 3405562703, 504530202, 3362938768, 1086122129, 3588952012, 177358838, 1668686040, 1788441005, 2920778456, 3450590302, 1707705043, 3940504028, 1650147200, 2144853533, 429939140, 2060161875, 226622212, 1271791848, 3603087696, 48155551, 966813043, 984177119, 3033759521, 3492815891, 2391190442, 3575857178, 3965974952, 459455113, 59851712, 416034666, 1727702234, 3862955095, 2038677741, 405912737, 3651584525, 1433865433, 4162114042, 319642522, 120211088, 3610217925, 1667950605, 284010502, 2536690859, 1757606927, 98163371, 1298766898, 2843598018, 2749694903, 3031345259, 2633279512, 2812045979, 34084905, 2989448216, 3311204930, 763257776, 747261640, 127287928, 326017657, 2610204813, 3746483709, 1345625337, 76875111, 1840566970, 4008707741, 1079217633, 5), None))88print(f" {[random<module 'random' from '/usr/local/lib/python3.12/random.py'>.randint(1, 10) for _ in range(3)]}") # Same8990# Random instance (separate state)91print("\nRandom instance:")92rng→ ⟨Random A⟩ = random<module 'random' from '/usr/local/lib/python3.12/random.py'>.Random(42)93print(f" {[rng⟨Random A⟩.randint(1, 100) for _ in range(5)]}")output Seeded random (seed=42): [82, 15, 4, 95, 36] [82, 15, 4, 95, 36] Save/restore state: [4, 4, 3] [4, 4, 3] Random instance: [82, 15, 4, 95, 36]
seed()
Initializes the random number generator to produce reproducible sequences - essential for testing and debugging randomized code.
Random Ranges
Generating numbers within specific ranges:
ranges.py
Replay: real traced execution (multi-file project)
# Random ranges and distributions
import random
random.seed(42)
# Random ranges
print("Random ranges:")
# Range [0, n)
print("Range [0, 100):")
print(f" {[random.randrange(100) for _ in range(10)]}")
# Range [a, b] inclusive
print("\nRange [10, 20] inclusive:")
print(f" {[random.randint(10, 20) for _ in range(10)]}")
# Range [a, b) exclusive
print("\nRange [10, 20) exclusive:")
print(f" {[random.randrange(10, 20) for _ in range(10)]}")
# Float range [a, b]
print("\nFloat range [0.0, 10.0]:")
print(f" {[random.uniform(0.0, 10.0) for _ in range(5)]}")
# Negative range
print("\nNegative range [-10, 10]:")
print(f" {[random.randint(-10, 10) for _ in range(10)]}")
# Dice roll (1-6)
print("\nDice rolls:")
rolls = [random.randint(1, 6) for _ in range(60)]
total_rolls = len(rolls)
for i in range(1, 7):
count = rolls.count(i)
percent = count / total_rolls * 100
print(f" {i}: {count} times ({percent:.1f}%)")
# Coin flip
print("\nCoin flips:")
flips = [random.choice(['H', 'T']) for _ in range(40)]
heads = flips.count('H')
tails = flips.count('T')
total_flips = len(flips)
print(f" Heads: {heads} ({heads / total_flips * 100:.1f}%)")
print(f" Tails: {tails} ({tails / total_flips * 100:.1f}%)")
# Weighted random
print("\nWeighted random:")
outcomes = ['common', 'uncommon', 'rare', 'epic']
weights = [50, 30, 15, 5]
results = random.choices(outcomes, weights=weights, k=40)
total_results = len(results)
for outcome in outcomes:
count = results.count(outcome)
percent = count / total_results * 100
print(f" {outcome}: {count} ({percent:.1f}%)")
# Probability check
print("\n20% probability:")
probability_trials = 40
successes = sum(1 for _ in range(probability_trials) if random.random() < 0.20)
print(f" Successes: {successes} ({successes / probability_trials * 100:.1f}%)")
# Random steps
print("\nRandom steps (2, 5, 10):")
steps = [random.choice([2, 5, 10]) for _ in range(10)]
print(f" {steps}")
# Multiples of 5
print("\nRandom multiples of 5 [0, 100):")
print(f" {[random.randrange(0, 100, 5) for _ in range(10)]}")
# Random percentage
print("\nRandom percentages:")
percentages = [random.uniform(0, 100) for _ in range(5)]
for p in percentages:
print(f" {p:.2f}%")
# Helper functions
def rand_range(a, b):
"""Random int in [a, b] inclusive."""
return random.randint(a, b)
def rand_float(a, b):
"""Random float in [a, b]."""
return random.uniform(a, b)
def probability(p):
"""Return True with probability p."""
return random.random() < p
print("\nHelper functions:")
print(f"rand_range(10, 20): {[rand_range(10, 20) for _ in range(5)]}")
print(f"rand_float(0, 1): {[rand_float(0, 1) for _ in range(5)]}")
print(f"probability(0.3): {[probability(0.3) for _ in range(10)]}")
rolls ← [5, 3, 1, 6, 4, 5, 1, 4, 1, 5, 3, 6, 5, 3, 5, 2, 6, 1, 1, 6, 2, 3, 1, 2, 1, 4, 3, 4, 6, 3, 2, 3, 3, 2, 6, 3, 6, 6, 6, 1, 5, 6, 2, 5, 6, 2, 2, 4, 4, 3, 6, 6, 5, 2, 6, 3, 1, 2, 1, 3]
3import random4random<module 'random' from '/usr/local/lib/python3.12/random.py'>.seed(42)56# Random ranges7print("Random ranges:")89# Range [0, n)10print("Range [0, 100):")11print(f" {[random<module 'random' from '/usr/local/lib/python3.12/random.py'>.randrange(100) for _ in range(10)]}")1213# Range [a, b] inclusive14print("\nRange [10, 20] inclusive:")15print(f" {[random<module 'random' from '/usr/local/lib/python3.12/random.py'>.randint(10, 20) for _ in range(10)]}")1617# Range [a, b) exclusive18print("\nRange [10, 20) exclusive:")19print(f" {[random<module 'random' from '/usr/local/lib/python3.12/random.py'>.randrange(10, 20) for _ in range(10)]}")2021# Float range [a, b]22print("\nFloat range [0.0, 10.0]:")23print(f" {[random<module 'random' from '/usr/local/lib/python3.12/random.py'>.uniform(0.0, 10.0) for _ in range(5)]}")2425# Negative range26print("\nNegative range [-10, 10]:")27print(f" {[random<module 'random' from '/usr/local/lib/python3.12/random.py'>.randint(-10, 10) for _ in range(10)]}")2829# Dice roll (1-6)30print("\nDice rolls:")31rolls→ [5, 3, 1, 6, 4, 5, 1, 4, 1, 5, 3, 6, 5, 3, 5, 2, 6, 1, 1, 6, 2, 3, 1, 2, 1, 4, 3, 4, 6, 3, 2, 3, 3, 2, 6, 3, 6, 6, 6, 1, 5, 6, 2, 5, 6, 2, 2, 4, 4, 3, 6, 6, 5, 2, 6, 3, 1, 2, 1, 3] = [random<module 'random' from '/usr/local/lib/python3.12/random.py'>.randint(1, 6) for _ in range(60)]32total_rolls→ 60 = len(rolls[5, 3, 1, 6, 4, 5, 1, 4, 1, 5, 3, 6, 5, 3, 5, 2, 6, 1, 1, 6, 2, 3, 1, 2, 1, 4, 3, 4, 6, 3, 2, 3, 3, 2, 6, 3, 6, 6, 6, 1, 5, 6, 2, 5, 6, 2, 2, 4, 4, 3, 6, 6, 5, 2, 6, 3, 1, 2, 1, 3])33for i in range(1, 7):outputRandom ranges: [81, 14, 3, 94, 35, 31, 28, 17, 94, 13] Range [10, 20] inclusive: [20, 18, 11, 19, 16, 10, 10, 11, 13, 13] [18, 19, 10, 18, 13, 18, 16, 13, 17, 19] Float range [0.0, 10.0]: [2.781907082306627, 8.69300320792934, 7.588073671297673, 1.5965931637689013, 4.226143981535025] Negative range [-10, 10]: [-2, -6, -4, 0, -7, -8, 2, -7, 1, 1] Dice rolls:count ← 10, percent ← 16.666666666666664
pass 1 of 632total_rolls = len(rolls)33for i1 in range(1, 7):34 count→ 10 = rolls[5, 3, 1, 6, 4, 5, 1, 4, 1, 5, 3, 6, 5, 3, 5, 2, 6, 1, 1, 6, 2, 3, 1, 2, 1, 4, 3, 4, 6, 3, 2, 3, 3, 2, 6, 3, 6, 6, 6, 1, 5, 6, 2, 5, 6, 2, 2, 4, 4, 3, 6, 6, 5, 2, 6, 3, 1, 2, 1, 3].count(i1)35 percent→ 16.666666666666664 = count10 / total_rolls60 * 10036 print(f" {i1}: {count10} times ({percent16.666666666666664:.1f}%)")output 1: 10 times (16.7%)All 6 passes — pass 1 is the card above pass icountpercent1 1 10 16.666666666666664 2 2 10 16.666666666666664 3 3 12 20.0 4 4 6 10.0 5 5 8 13.333333333333334 6 6 14 23.333333333333332 flips ← ['T', 'T', 'H', 'H', 'T', 'H', 'T', 'T', 'T', 'H', 'T', 'H', 'H', 'T', 'T', 'T', 'T', 'H', 'H', 'T', 'H', 'H', 'H', 'H', 'H', 'T', 'H', 'T', 'T', 'T', 'T', 'H', 'H', 'T', 'T', 'H', 'T', 'T', 'H', 'T']
38# Coin flip39print("\nCoin flips:")40flips→ ['T', 'T', 'H', 'H', 'T', 'H', 'T', 'T', 'T', 'H', 'T', 'H', 'H', 'T', 'T', 'T', 'T', 'H', 'H', 'T', 'H', 'H', 'H', 'H', 'H', 'T', 'H', 'T', 'T', 'T', 'T', 'H', 'H', 'T', 'T', 'H', 'T', 'T', 'H', 'T'] = [random<module 'random' from '/usr/local/lib/python3.12/random.py'>.choice(['H', 'T']) for _ in range(40)]41heads→ 18 = flips['T', 'T', 'H', 'H', 'T', 'H', 'T', 'T', 'T', 'H', 'T', 'H', 'H', 'T', 'T', 'T', 'T', 'H', 'H', 'T', 'H', 'H', 'H', 'H', 'H', 'T', 'H', 'T', 'T', 'T', 'T', 'H', 'H', 'T', 'T', 'H', 'T', 'T', 'H', 'T'].count('H')42tails→ 22 = flips['T', 'T', 'H', 'H', 'T', 'H', 'T', 'T', 'T', 'H', 'T', 'H', 'H', 'T', 'T', 'T', 'T', 'H', 'H', 'T', 'H', 'H', 'H', 'H', 'H', 'T', 'H', 'T', 'T', 'T', 'T', 'H', 'H', 'T', 'T', 'H', 'T', 'T', 'H', 'T'].count('T')43total_flips→ 40 = len(flips['T', 'T', 'H', 'H', 'T', 'H', 'T', 'T', 'T', 'H', 'T', 'H', 'H', 'T', 'T', 'T', 'T', 'H', 'H', 'T', 'H', 'H', 'H', 'H', 'H', 'T', 'H', 'T', 'T', 'T', 'T', 'H', 'H', 'T', 'T', 'H', 'T', 'T', 'H', 'T'])44print(f" Heads: {heads18} ({heads / total_flips40 * 100:.1f}%)")45print(f" Tails: {tails22} ({tails / total_flips40 * 100:.1f}%)")4647# Weighted random48print("\nWeighted random:")49outcomes→ ['common', 'uncommon', 'rare', 'epic'] = ['common', 'uncommon', 'rare', 'epic']50weights→ [50, 30, 15, 5] = [50, 30, 15, 5]51results→ ['common', 'uncommon', 'uncommon', 'epic', 'uncommon', 'uncommon', 'common', 'uncommon', 'rare', 'uncommon', 'common', 'common', 'common', 'epic', 'rare', 'rare', 'uncommon', 'common', 'common', 'common', 'epic', 'rare', 'common', 'common', 'uncommon', 'common', 'uncommon', 'rare', 'epic', 'uncommon', 'common', 'uncommon', 'rare', 'common', 'uncommon', 'uncommon', 'epic', 'rare', 'uncommon', 'uncommon'] = random<module 'random' from '/usr/local/lib/python3.12/random.py'>.choices(outcomes['common', 'uncommon', 'rare', 'epic'], weights=weights[50, 30, 15, 5], k=40)52total_results→ 40 = len(results['common', 'uncommon', 'uncommon', 'epic', 'uncommon', 'uncommon', 'common', 'uncommon', 'rare', 'uncommon', 'common', 'common', 'common', 'epic', 'rare', 'rare', 'uncommon', 'common', 'common', 'common', 'epic', 'rare', 'common', 'common', 'uncommon', 'common', 'uncommon', 'rare', 'epic', 'uncommon', 'common', 'uncommon', 'rare', 'common', 'uncommon', 'uncommon', 'epic', 'rare', 'uncommon', 'uncommon'])53for outcome in outcomes:output Coin flips: Heads: 18 (45.0%) Tails: 22 (55.0%) Weighted random:count ← 13, percent ← 32.5
pass 1 of 452total_results = len(results)53for outcomecommon in outcomes['common', 'uncommon', 'rare', 'epic']:54 count→ 13 = results['common', 'uncommon', 'uncommon', 'epic', 'uncommon', 'uncommon', 'common', 'uncommon', 'rare', 'uncommon', 'common', 'common', 'common', 'epic', 'rare', 'rare', 'uncommon', 'common', 'common', 'common', 'epic', 'rare', 'common', 'common', 'uncommon', 'common', 'uncommon', 'rare', 'epic', 'uncommon', 'common', 'uncommon', 'rare', 'common', 'uncommon', 'uncommon', 'epic', 'rare', 'uncommon', 'uncommon'].count(outcomecommon)55 percent→ 32.5 = count13 / total_results40 * 10056 print(f" {outcomecommon}: {count13} ({percent32.5:.1f}%)")output common: 13 (32.5%)All 4 passes — pass 1 is the card above pass outcomecountpercent1 common 13 32.5 2 uncommon 15 37.5 3 rare 7 17.5 4 epic 5 12.5 probability_trials ← 40, successes ← 11, steps ← [10, 5, 2, 5, 2, 5, 5, 2, 2, 5]
58# Probability check59print("\n20% probability:")60probability_trials→ 40 = 4061successes→ 11 = sum(1 for _ in range(probability_trials40) if random<module 'random' from '/usr/local/lib/python3.12/random.py'>.random() < 0.20)62print(f" Successes: {successes11} ({successes / probability_trials40 * 100:.1f}%)")6364# Random steps65print("\nRandom steps (2, 5, 10):")66steps→ [10, 5, 2, 5, 2, 5, 5, 2, 2, 5] = [random<module 'random' from '/usr/local/lib/python3.12/random.py'>.choice([2, 5, 10]) for _ in range(10)]67print(f" {steps[10, 5, 2, 5, 2, 5, 5, 2, 2, 5]}")6869# Multiples of 570print("\nRandom multiples of 5 [0, 100):")71print(f" {[random<module 'random' from '/usr/local/lib/python3.12/random.py'>.randrange(0, 100, 5) for _ in range(10)]}")7273# Random percentage74print("\nRandom percentages:")75percentages→ [48.13584179710395, 86.46503940302155, 90.24428655105487, 16.463712140152687, 0.2155378351426429] = [random<module 'random' from '/usr/local/lib/python3.12/random.py'>.uniform(0, 100) for _ in range(5)]76for p in percentages:output 20% probability: Successes: 11 (27.5%) Random steps (2, 5, 10): [10, 5, 2, 5, 2, 5, 5, 2, 2, 5] [85, 15, 5, 85, 0, 10, 35, 25, 65, 75] Random percentages:for p in percentages:
pass 1 of 575percentages = [random.uniform(0, 100) for _ in range(5)]76for p48.13584179710395 in percentages[48.13584179710395, 86.46503940302155, 90.24428655105487, 16.463712140152687, 0.2155378351426429]:77 print(f" {p48.13584179710395:.2f}%")output 48.14%All 5 passes — pass 1 is the card above pass p1 48.13584179710395 2 86.46503940302155 3 90.24428655105487 4 16.463712140152687 5 0.2155378351426429 print(" Helper functions:")
92print("\nHelper functions:")93print(f"rand_range(10, 20): {[rand_range(10, 20) for _ in range(5)]}")94print(f"rand_float(0, 1): {[rand_float(0, 1) for _ in range(5)]}")output Helper functions:def rand_range(a, b):
pass 1 of 579# Helper functions80def rand_range(a10, b20):81 """Random int in [a, b] inclusive."""82 return random<module 'random' from '/usr/local/lib/python3.12/random.py'>.randint(a10, b20)print(f"rand_range(10, 20): {[rand_range(10, 20) for _ in range(5)]}")
92print("\nHelper functions:")93print(f"rand_range(10, 20): {[rand_range(10, 20) for _ in range(5)]}")94print(f"rand_float(0, 1): {[rand_float(0, 1) for _ in range(5)]}")95print(f"probability(0.3): {[probability(0.3) for _ in range(10)]}")outputrand_range(10, 20): [16, 14, 17, 14, 16]def rand_float(a, b):
pass 1 of 584def rand_float(a0, b1):85 """Random float in [a, b]."""86 return random<module 'random' from '/usr/local/lib/python3.12/random.py'>.uniform(a0, b1)print(f"rand_float(0, 1): {[rand_float(0, 1) for _ in range(5)]}")
93print(f"rand_range(10, 20): {[rand_range(10, 20) for _ in range(5)]}")94print(f"rand_float(0, 1): {[rand_float(0, 1) for _ in range(5)]}")95print(f"probability(0.3): {[probability(0.3) for _ in range(10)]}")outputrand_float(0, 1): [0.6965915036001625, 0.730505317415466, 0.7833615284842872, 0.6618713119507452, 0.4866714116543295]def probability(p):
pass 1 of 1088def probability(p0.3):89 """Return True with probability p."""90 return random<module 'random' from '/usr/local/lib/python3.12/random.py'>.random() < p0.3print(f"probability(0.3): {[probability(0.3) for _ in range(10)]}")
94print(f"rand_float(0, 1): {[rand_float(0, 1) for _ in range(5)]}")95print(f"probability(0.3): {[probability(0.3) for _ in range(10)]}")outputprobability(0.3): [True, True, True, False, True, False, True, False, False, False]
Random Selection and Shuffling
Choosing and rearranging elements:
shuffle.py
Replay: real traced execution (multi-file project)
# Shuffling and sampling
import random
random.seed(42)
# Shuffling
print("Shuffling:")
# Shuffle list (in-place)
names = ['Alice', 'Bob', 'Charlie', 'David', 'Eve']
print(f"Original: {names}")
random.shuffle(names)
print(f"Shuffled: {names}")
# Shuffle with seed
print("\nShuffle with seed:")
nums1 = list(range(1, 11))
nums2 = list(range(1, 11))
random.seed(42)
random.shuffle(nums1)
random.seed(42)
random.shuffle(nums2)
print(f"First: {nums1}")
print(f"Second: {nums2}")
print(f"Same: {nums1 == nums2}")
# Sample (without replacement)
print("\nSample (without replacement):")
population = list(range(1, 11))
for _ in range(5):
sample = random.sample(population, k=3)
print(f" {sample}")
# Choices (with replacement)
print("\nChoices (with replacement):")
for _ in range(5):
choices = random.choices(population, k=3)
print(f" {choices}")
# Sample entire population (shuffle alternative)
print("\nSample entire population:")
shuffled = random.sample(population, k=len(population))
print(f" {shuffled}")
# Pick random element
print("\nPick random element:")
colors = ['red', 'green', 'blue', 'yellow', 'purple']
for _ in range(10):
color = random.choice(colors)
print(f" {color}", end=" ")
print()
# Weighted sampling
print("\nWeighted sampling:")
items = ['common', 'uncommon', 'rare', 'legendary']
weights = [50, 30, 15, 5]
print("Weighted choices (k=10):")
for _ in range(5):
result = random.choices(items, weights=weights, k=10)
print(f" {result}")
# Random permutation
print("\nRandom permutations of 'ABCD':")
chars = ['A', 'B', 'C', 'D']
for _ in range(5):
perm = random.sample(chars, k=len(chars))
print(f" {''.join(perm)}")
# Deal cards
print("\nDeal cards:")
suits = ['♠', '♥', '♦', '♣']
ranks = ['A', '2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K']
deck = [f"{rank}{suit}" for suit in suits for rank in ranks]
random.shuffle(deck)
print(f"Original (first 13): {deck[:13]}")
# Deal hands
print("\nDeal 4 hands of 5 cards:")
for player in range(4):
hand = deck[player*5:(player+1)*5]
print(f" Player {player+1}: {hand}")
# Random subset
print("\nRandom subsets:")
data = list(range(1, 21))
for size in [3, 5, 10]:
subset = random.sample(data, k=size)
print(f" Size {size}: {subset}")
# Random pairs
print("\nRandom pairs:")
people = ['Alice', 'Bob', 'Charlie', 'David', 'Eve', 'Frank']
shuffled = random.sample(people, k=len(people))
pairs = [(shuffled[i], shuffled[i+1]) for i in range(0, len(shuffled)-1, 2)]
for pair in pairs:
print(f" {pair[0]} <-> {pair[1]}")
names ← ['Alice', 'Bob', 'Charlie', 'David', 'Eve'], nums1 ← [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
3import random4random<module 'random' from '/usr/local/lib/python3.12/random.py'>.seed(42)56# Shuffling7print("Shuffling:")89# Shuffle list (in-place)10names→ ['Alice', 'Bob', 'Charlie', 'David', 'Eve'] = ['Alice', 'Bob', 'Charlie', 'David', 'Eve']11print(f"Original: {names['Alice', 'Bob', 'Charlie', 'David', 'Eve']}")12random<module 'random' from '/usr/local/lib/python3.12/random.py'>.shuffle(names→ ['David', 'Bob', 'Charlie', 'Eve', 'Alice'])13print(f"Shuffled: {names['David', 'Bob', 'Charlie', 'Eve', 'Alice']}")1415# Shuffle with seed16print("\nShuffle with seed:")17nums1→ [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] = list(range(1, 11))18nums2→ [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] = list(range(1, 11))1920random<module 'random' from '/usr/local/lib/python3.12/random.py'>.seed(42)21random<module 'random' from '/usr/local/lib/python3.12/random.py'>.shuffle(nums1→ [8, 4, 3, 9, 6, 7, 10, 5, 1, 2])22random<module 'random' from '/usr/local/lib/python3.12/random.py'>.seed(42)23random<module 'random' from '/usr/local/lib/python3.12/random.py'>.shuffle(nums2→ [8, 4, 3, 9, 6, 7, 10, 5, 1, 2])2425print(f"First: {nums1[8, 4, 3, 9, 6, 7, 10, 5, 1, 2]}")26print(f"Second: {nums2[8, 4, 3, 9, 6, 7, 10, 5, 1, 2]}")27print(f"Same: {nums1[8, 4, 3, 9, 6, 7, 10, 5, 1, 2] == nums2[8, 4, 3, 9, 6, 7, 10, 5, 1, 2]}")2829# Sample (without replacement)30print("\nSample (without replacement):")31population→ [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] = list(range(1, 11))32for _ in range(5):outputShuffling: Original: ['Alice', 'Bob', 'Charlie', 'David', 'Eve'] Shuffled: ['David', 'Bob', 'Charlie', 'Eve', 'Alice'] Shuffle with seed: First: [8, 4, 3, 9, 6, 7, 10, 5, 1, 2] Second: [8, 4, 3, 9, 6, 7, 10, 5, 1, 2] Same: True Sample (without replacement):sample ← [10, 7, 1]
pass 1 of 531population = list(range(1, 11))32for _0 in range(5):33 sample→ [10, 7, 1] = random<module 'random' from '/usr/local/lib/python3.12/random.py'>.sample(population[1, 2, 3, 4, 5, 6, 7, 8, 9, 10], k=3)34 print(f" {sample[10, 7, 1]}")output [10, 7, 1]All 5 passes — pass 1 is the card above pass _sample1 0 [10, 7, 1] 2 1 [1, 2, 4] 3 2 [4, 9, 1] 4 3 [9, 4, 7] 5 4 [4, 8, 5] print(" Choices (with replacement):")
36# Choices (with replacement)37print("\nChoices (with replacement):")38for _ in range(5):output Choices (with replacement):choices ← [9, 1, 9]
pass 1 of 537print("\nChoices (with replacement):")38for _0 in range(5):39 choices→ [9, 1, 9] = random<module 'random' from '/usr/local/lib/python3.12/random.py'>.choices(population[1, 2, 3, 4, 5, 6, 7, 8, 9, 10], k=3)40 print(f" {choices[9, 1, 9]}")output [9, 1, 9]All 5 passes — pass 1 is the card above pass _choices1 0 [9, 1, 9] 2 1 [7, 4, 2] 3 2 [10, 4, 1] 4 3 [1, 9, 7] 5 4 [9, 8, 6] shuffled ← [7, 2, 5, 10, 6, 8, 3, 4, 1, 9], colors ← ['red', 'green', 'blue', 'yellow', 'purple']
42# Sample entire population (shuffle alternative)43print("\nSample entire population:")44shuffled→ [7, 2, 5, 10, 6, 8, 3, 4, 1, 9] = random<module 'random' from '/usr/local/lib/python3.12/random.py'>.sample(population[1, 2, 3, 4, 5, 6, 7, 8, 9, 10], k=len(population))45print(f" {shuffled[7, 2, 5, 10, 6, 8, 3, 4, 1, 9]}")4647# Pick random element48print("\nPick random element:")49colors→ ['red', 'green', 'blue', 'yellow', 'purple'] = ['red', 'green', 'blue', 'yellow', 'purple']50for _ in range(10):output Sample entire population: [7, 2, 5, 10, 6, 8, 3, 4, 1, 9] Pick random element:color ← red
pass 1 of 1049colors = ['red', 'green', 'blue', 'yellow', 'purple']50for _0 in range(10):51 color→ red = random<module 'random' from '/usr/local/lib/python3.12/random.py'>.choice(colors['red', 'green', 'blue', 'yellow', 'purple'])52 print(f" {colorred}", end=" ")53print()output redAll 10 passes — pass 1 is the card above pass _color1 0 red 2 1 green 3 2 blue 4 3 red 5 4 green 6 5 red 7 6 yellow 8 7 blue 9 8 yellow 10 9 blue items ← ['common', 'uncommon', 'rare', 'legendary'], weights ← [50, 30, 15, 5]
52 print(f" {color}", end=" ")53print()5455# Weighted sampling56print("\nWeighted sampling:")57items→ ['common', 'uncommon', 'rare', 'legendary'] = ['common', 'uncommon', 'rare', 'legendary']58weights→ [50, 30, 15, 5] = [50, 30, 15, 5]5960print("Weighted choices (k=10):")61for _ in range(5):output Weighted sampling: Weighted choices (k=10):result ← ['common', 'common', 'uncommon', 'uncommon', 'uncommon', 'common', 'uncommon', 'uncommon', 'common', 'common']
pass 1 of 560print("Weighted choices (k=10):")61for _0 in range(5):62 result→ ['common', 'common', 'uncommon', 'uncommon', 'uncommon', 'common', 'uncommon', 'uncommon', 'common', 'common'] = random<module 'random' from '/usr/local/lib/python3.12/random.py'>.choices(items['common', 'uncommon', 'rare', 'legendary'], weights=weights[50, 30, 15, 5], k=10)63 print(f" {result['common', 'common', 'uncommon', 'uncommon', 'uncommon', 'common', 'uncommon', 'uncommon', 'common', 'common']}")output ['common', 'common', 'uncommon', 'uncommon', 'uncommon', 'common', 'uncommon', 'uncommon', 'common', 'common']All 5 passes — pass 1 is the card above pass _result1 0 ['common', 'common', 'uncommon', 'uncommon', 'uncommon', 'common', 'uncommon', 'uncommon', 'common', 'common'] 2 1 ['common', 'rare', 'uncommon', 'common', 'common', 'uncommon', 'common', 'rare', 'rare', 'common'] 3 2 ['common', 'rare', 'uncommon', 'uncommon', 'common', 'common', 'rare', 'uncommon', 'common', 'common'] 4 3 ['uncommon', 'uncommon', 'uncommon', 'common', 'uncommon', 'common', 'legendary', 'common', 'common', 'uncommon'] 5 4 ['rare', 'common', 'common', 'uncommon', 'uncommon', 'common', 'uncommon', 'common', 'common', 'uncommon'] chars ← ['A', 'B', 'C', 'D']
65# Random permutation66print("\nRandom permutations of 'ABCD':")67chars→ ['A', 'B', 'C', 'D'] = ['A', 'B', 'C', 'D']68for _ in range(5):output Random permutations of 'ABCD':perm ← ['A', 'C', 'D', 'B']
pass 1 of 567chars = ['A', 'B', 'C', 'D']68for _0 in range(5):69 perm→ ['A', 'C', 'D', 'B'] = random<module 'random' from '/usr/local/lib/python3.12/random.py'>.sample(chars['A', 'B', 'C', 'D'], k=len(chars))70 print(f" {''.join(perm['A', 'C', 'D', 'B'])}")output ACDBAll 5 passes — pass 1 is the card above pass _perm1 0 ['A', 'C', 'D', 'B'] 2 1 ['C', 'A', 'B', 'D'] 3 2 ['B', 'D', 'A', 'C'] 4 3 ['B', 'C', 'A', 'D'] 5 4 ['B', 'A', 'D', 'C'] suits ← ['♠', '♥', '♦', '♣'], ranks ← ['A', '2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K']
72# Deal cards73print("\nDeal cards:")74suits→ ['♠', '♥', '♦', '♣'] = ['♠', '♥', '♦', '♣']75ranks→ ['A', '2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K'] = ['A', '2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K']76deck→ ['A♠', '2♠', '3♠', '4♠', '5♠', '6♠', '7♠', '8♠', '9♠', '10♠', 'J♠', 'Q♠', 'K♠', 'A♥', '2♥', '3♥', '4♥', '5♥', '6♥', '7♥', '8♥', '9♥', '10♥', 'J♥', 'Q♥', 'K♥', 'A♦', '2♦', '3♦', '4♦', '5♦', '6♦', '7♦', '8♦', '9♦', '10♦', 'J♦', 'Q♦', 'K♦', 'A♣', '2♣', '3♣', '4♣', '5♣', '6♣', '7♣', '8♣', '9♣', '10♣', 'J♣', 'Q♣', 'K♣'] = [f"{rank}{suit}" for suit in suits['♠', '♥', '♦', '♣'] for rank in ranks['A', '2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K']]7778random<module 'random' from '/usr/local/lib/python3.12/random.py'>.shuffle(deck→ ['J♠', '5♣', '3♠', '8♣', 'Q♥', '5♦', 'Q♣', '6♥', 'A♦', '10♣', '2♦', '4♦', '4♣', 'A♣', '6♣', 'K♥', '2♥', 'Q♠', 'K♠', '10♠', '10♥', '3♦', '7♠', 'A♥', '3♣', '4♥', 'K♣', '10♦', '5♥', '9♣', '9♥', '7♦', '9♠', '5♠', '7♣', 'Q♦', '6♠', 'J♦', '2♣', '4♠', '3♥', '7♥', 'J♥', '8♠', '2♠', '6♦', '8♥', 'K♦', 'A♠', '8♦', 'J♣', '9♦'])79print(f"Original (first 13): {deck[:13]['J♠', '5♣', '3♠', '8♣', 'Q♥', '5♦', 'Q♣', '6♥', 'A♦', '10♣', '2♦', '4♦', '4♣']}")8081# Deal hands82print("\nDeal 4 hands of 5 cards:")83for player in range(4):output Deal cards: Original (first 13): ['J♠', '5♣', '3♠', '8♣', 'Q♥', '5♦', 'Q♣', '6♥', 'A♦', '10♣', '2♦', '4♦', '4♣'] Deal 4 hands of 5 cards:hand ← ['J♠', '5♣', '3♠', '8♣', 'Q♥']
pass 1 of 482print("\nDeal 4 hands of 5 cards:")83for player0 in range(4):84 hand→ ['J♠', '5♣', '3♠', '8♣', 'Q♥'] = deck[player*5:(player+1)*5]['J♠', '5♣', '3♠', '8♣', 'Q♥']85 print(f" Player {player0+1}: {hand['J♠', '5♣', '3♠', '8♣', 'Q♥']}")output Player 1: ['J♠', '5♣', '3♠', '8♣', 'Q♥']All 4 passes — pass 1 is the card above pass playerdeck[player*5:(player+1)*5]hand1 0 ['J♠', '5♣', '3♠', '8♣', 'Q♥'] ['J♠', '5♣', '3♠', '8♣', 'Q♥'] 2 1 ['5♦', 'Q♣', '6♥', 'A♦', '10♣'] ['5♦', 'Q♣', '6♥', 'A♦', '10♣'] 3 2 ['2♦', '4♦', '4♣', 'A♣', '6♣'] ['2♦', '4♦', '4♣', 'A♣', '6♣'] 4 3 ['K♥', '2♥', 'Q♠', 'K♠', '10♠'] ['K♥', '2♥', 'Q♠', 'K♠', '10♠'] data ← [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20]
87# Random subset88print("\nRandom subsets:")89data→ [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20] = list(range(1, 21))90for size in [3, 5, 10]:output Random subsets:subset ← [8, 3, 2]
pass 1 of 389data = list(range(1, 21))90for size3 in [3, 5, 10]:91 subset→ [8, 3, 2] = random<module 'random' from '/usr/local/lib/python3.12/random.py'>.sample(data[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20], k=size3)92 print(f" Size {size3}: {subset[8, 3, 2]}")output Size 3: [8, 3, 2]All 3 passes — pass 1 is the card above pass sizesubset1 3 [8, 3, 2] 2 5 [11, 3, 17, 8, 9] 3 10 [16, 7, 18, 5, 20, 4, 13, 8, 19, 15] people ← ['Alice', 'Bob', 'Charlie', 'David', 'Eve', 'Frank']
94# Random pairs95print("\nRandom pairs:")96people→ ['Alice', 'Bob', 'Charlie', 'David', 'Eve', 'Frank'] = ['Alice', 'Bob', 'Charlie', 'David', 'Eve', 'Frank']97shuffled→ ['Alice', 'Frank', 'David', 'Bob', 'Charlie', 'Eve'] = random<module 'random' from '/usr/local/lib/python3.12/random.py'>.sample(people['Alice', 'Bob', 'Charlie', 'David', 'Eve', 'Frank'], k=len(people))98pairs→ [('Alice', 'Frank'), ('David', 'Bob'), ('Charlie', 'Eve')] = [(shuffled[i](empty), shuffled[i+1](empty)) for i in range(0, len(shuffled['Alice', 'Frank', 'David', 'Bob', 'Charlie', 'Eve'])-1, 2)]99for pair in pairs:output Random pairs:for pair in pairs:
pass 1 of 398pairs = [(shuffled[i], shuffled[i+1]) for i in range(0, len(shuffled)-1, 2)]99for pair('Alice', 'Frank') in pairs[('Alice', 'Frank'), ('David', 'Bob'), ('Charlie', 'Eve')]:100 print(f" {pair[0]Alice} <-> {pair[1]Frank}")output Alice <-> FrankAll 3 passes — pass 1 is the card above pass pairpair[0]pair[1]1 ('Alice', 'Frank') Alice Frank 2 ('David', 'Bob') David Bob 3 ('Charlie', 'Eve') Charlie Eve
choice vs sample
choice() picks one item with replacement, sample() picks multiple unique items without replacement, choices() picks multiple with replacement.
Random Strings
Generating random strings for identifiers:
strings.py
Replay: real traced execution (multi-file project)
# Random strings and data
import random
import string
random.seed(42)
# Random strings
print("Random strings:")
# Random letters
print("Random letters:")
letters = [random.choice(string.ascii_lowercase) for _ in range(10)]
print(f" {''.join(letters)}")
# Random uppercase
print("\nRandom uppercase:")
uppercase = [random.choice(string.ascii_uppercase) for _ in range(10)]
print(f" {''.join(uppercase)}")
# Random digits
print("\nRandom digits:")
digits = [random.choice(string.digits) for _ in range(10)]
print(f" {''.join(digits)}")
# Random alphanumeric
print("\nRandom alphanumeric:")
def random_alphanumeric(length):
"""Generate random alphanumeric string."""
return ''.join(random.choices(string.ascii_letters + string.digits, k=length))
for _ in range(5):
print(f" {random_alphanumeric(12)}")
# Random password
print("\nRandom passwords:")
def random_password(length=12):
"""Generate random password with all character types."""
chars = string.ascii_letters + string.digits + string.punctuation
while True:
password = ''.join(random.choices(chars, k=length))
# Ensure at least one of each type
if (any(c.islower() for c in password) and
any(c.isupper() for c in password) and
any(c.isdigit() for c in password) and
any(c in string.punctuation for c in password)):
return password
for _ in range(5):
print(f" {random_password(16)}")
# Random hex
print("\nRandom hex strings:")
def random_hex(length):
"""Generate random hex string."""
return ''.join(random.choices('0123456789abcdef', k=length))
for _ in range(5):
print(f" {random_hex(16)}")
# Random UUID-like
print("\nRandom UUID-like:")
def random_uuid():
"""Generate random UUID-like string."""
return f"{random_hex(8)}-{random_hex(4)}-{random_hex(4)}-{random_hex(4)}-{random_hex(12)}"
for _ in range(3):
print(f" {random_uuid()}")
# Random email
print("\nRandom emails:")
def random_email():
"""Generate random email address."""
username = ''.join(random.choices(string.ascii_lowercase, k=8))
domain = random.choice(['gmail.com', 'yahoo.com', 'hotmail.com', 'example.com'])
return f"{username}@{domain}"
for _ in range(5):
print(f" {random_email()}")
# Random phone
print("\nRandom phone numbers:")
def random_phone():
"""Generate random phone number."""
area = random.randint(200, 999)
exchange = random.randint(200, 999)
number = random.randint(0, 9999)
return f"({area:03d}) {exchange:03d}-{number:04d}"
for _ in range(5):
print(f" {random_phone()}")
# Random name
print("\nRandom names:")
first_names = ['Alice', 'Bob', 'Charlie', 'Diana', 'Eve', 'Frank']
last_names = ['Smith', 'Johnson', 'Williams', 'Brown', 'Jones', 'Miller']
for _ in range(5):
name = f"{random.choice(first_names)} {random.choice(last_names)}"
print(f" {name}")
# Random date
print("\nRandom dates:")
import datetime
def random_date(start_year=2020, end_year=2024):
"""Generate random date."""
year = random.randint(start_year, end_year)
month = random.randint(1, 12)
day = random.randint(1, 28) # Simplified
return datetime.date(year, month, day)
for _ in range(5):
print(f" {random_date()}")
# Random color
print("\nRandom RGB colors:")
def random_color():
"""Generate random RGB color."""
r = random.randint(0, 255)
g = random.randint(0, 255)
b = random.randint(0, 255)
return f"#{r:02x}{g:02x}{b:02x}"
for _ in range(5):
print(f" {random_color()}")
letters ← ['u', 'd', 'a', 'x', 'i', 'h', 'h', 'e', 'x', 'd'], uppercase ← ['V', 'X', 'R', 'C', 'S', 'N', 'B', 'A', 'C', 'G']
4import string5random<module 'random' from '/usr/local/lib/python3.12/random.py'>.seed(42)67# Random strings8print("Random strings:")910# Random letters11print("Random letters:")12letters→ ['u', 'd', 'a', 'x', 'i', 'h', 'h', 'e', 'x', 'd'] = [random<module 'random' from '/usr/local/lib/python3.12/random.py'>.choice(string.ascii_lowercaseabcdefghijklmnopqrstuvwxyz) for _ in range(10)]13print(f" {''.join(letters['u', 'd', 'a', 'x', 'i', 'h', 'h', 'e', 'x', 'd'])}")1415# Random uppercase16print("\nRandom uppercase:")17uppercase→ ['V', 'X', 'R', 'C', 'S', 'N', 'B', 'A', 'C', 'G'] = [random<module 'random' from '/usr/local/lib/python3.12/random.py'>.choice(string.ascii_uppercaseABCDEFGHIJKLMNOPQRSTUVWXYZ) for _ in range(10)]18print(f" {''.join(uppercase['V', 'X', 'R', 'C', 'S', 'N', 'B', 'A', 'C', 'G'])}")1920# Random digits21print("\nRandom digits:")22digits→ ['3', '8', '9', '0', '8', '3', '8', '6', '3', '7'] = [random<module 'random' from '/usr/local/lib/python3.12/random.py'>.choice(string.digits0123456789) for _ in range(10)]23print(f" {''.join(digits['3', '8', '9', '0', '8', '3', '8', '6', '3', '7'])}")2425# Random alphanumeric26print("\nRandom alphanumeric:")27def random_alphanumeric(length):outputRandom strings: Random letters: udaxihhexd Random uppercase: VXRCSNBACG Random digits: 3890838637 Random alphanumeric:for _ in range(5):
pass 1 of 531for _0 in range(5):32 print(f" {random_alphanumeric(12)}")All 5 passes — pass 1 is the card above pass _1 0 2 1 3 2 4 3 5 4 def random_alphanumeric(length):
pass 1 of 526print("\nRandom alphanumeric:")27def random_alphanumeric(length12):28 """Generate random alphanumeric string."""29 return ''.join(random<module 'random' from '/usr/local/lib/python3.12/random.py'>.choices(string.ascii_lettersabcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ + string.digits0123456789, k=length12))print(f" {random_alphanumeric(12)}")
31for _ in range(5):32 print(f" {random_alphanumeric(12)}")output KYaXRvj7uff0print(f" {random_alphanumeric(12)}")
31for _ in range(5):32 print(f" {random_alphanumeric(12)}")output LYTH8xIZM1JRprint(f" {random_alphanumeric(12)}")
31for _ in range(5):32 print(f" {random_alphanumeric(12)}")output coreogrNwwmqprint(f" {random_alphanumeric(12)}")
31for _ in range(5):32 print(f" {random_alphanumeric(12)}")output 6OLkTkx9NIQ0print(f" {random_alphanumeric(12)}")
31for _ in range(5):32 print(f" {random_alphanumeric(12)}")output Wobtqn62tOy4print(" Random passwords:")
34# Random password35print("\nRandom passwords:")36def random_password(length=12):output Random passwords:for _ in range(5):
pass 1 of 548for _0 in range(5):49 print(f" {random_password(16)}")All 5 passes — pass 1 is the card above pass _1 0 2 1 3 2 4 3 5 4 chars ← abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!"#$%&'()*+,-./:;<=>?@[\]^_`{|}~
pass 1 of 535print("\nRandom passwords:")36def random_password(length16=12):37 """Generate random password with all character types."""38 chars→ abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!"#$%&'()*+,-./:;<=>?@[\]^_`{|}~ = string.ascii_lettersabcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ + string.digits0123456789 + string.punctuation!"#$%&'()*+,-./:;<=>?@[\]^_`{|}~39 while True:All 5 passes — pass 1 is the card above pass chars1 abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!"#$%&'()*+,-./:;<=>?@[\]^_`{|}~ 2 abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!"#$%&'()*+,-./:;<=>?@[\]^_`{|}~ 3 abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!"#$%&'()*+,-./:;<=>?@[\]^_`{|}~ 4 abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!"#$%&'()*+,-./:;<=>?@[\]^_`{|}~ 5 abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!"#$%&'()*+,-./:;<=>?@[\]^_`{|}~ password ← Ryx0y2[Lu~Viek6-
pass 1 of 638chars = string.ascii_letters + string.digits + string.punctuation39while True:40 password→ Ryx0y2[Lu~Viek6- = ''.join(random<module 'random' from '/usr/local/lib/python3.12/random.py'>.choices(charsabcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!"#$%&'()*+,-./:;<=>?@[\]^_`{|}~, k=length16))41 # Ensure at least one of each typeAll 6 passes — pass 1 is the card above pass password1 Ryx0y2[Lu~Viek6- 2 NfJ~X|=b&#Yz8kOQ 3 `?yVq\>C85o*Y,Xa 4 Eb^?;Cf?`iTg**mS 5 Zy?NtY'sD~9PWlvF 6 3vug7v\=gw!um^1S if (any(c.islower() for c in password) and any(c.isupper()…
pass 1 of 541# Ensure at least one of each type42if (any(c.islower() for c in passwordRyx0y2[Lu~Viek6-) and43 any(c.isupper() for c in passwordRyx0y2[Lu~Viek6-) and44 any(c.isdigit() for c in passwordRyx0y2[Lu~Viek6-) and45 any(c in string.punctuation!"#$%&'()*+,-./:;<=>?@[\]^_`{|}~ for c in passwordRyx0y2[Lu~Viek6-)):46 return passwordRyx0y2[Lu~Viek6-All 5 passes — pass 1 is the card above pass password1 Ryx0y2[Lu~Viek6- 2 NfJ~X|=b&#Yz8kOQ 3 `?yVq\>C85o*Y,Xa 4 Zy?NtY'sD~9PWlvF 5 3vug7v\=gw!um^1S print(f" {random_password(16)}")
48for _ in range(5):49 print(f" {random_password(16)}")output Ryx0y2[Lu~Viek6-print(f" {random_password(16)}")
48for _ in range(5):49 print(f" {random_password(16)}")output NfJ~X|=b&#Yz8kOQprint(f" {random_password(16)}")
48for _ in range(5):49 print(f" {random_password(16)}")output `?yVq\>C85o*Y,Xaprint(f" {random_password(16)}")
48for _ in range(5):49 print(f" {random_password(16)}")output Zy?NtY'sD~9PWlvFprint(f" {random_password(16)}")
48for _ in range(5):49 print(f" {random_password(16)}")output 3vug7v\=gw!um^1Sprint(" Random hex strings:")
51# Random hex52print("\nRandom hex strings:")53def random_hex(length):output Random hex strings:for _ in range(5):
pass 1 of 557for _0 in range(5):58 print(f" {random_hex(16)}")All 5 passes — pass 1 is the card above pass _1 0 2 1 3 2 4 3 5 4 def random_hex(length):
pass 1 of 2052print("\nRandom hex strings:")53def random_hex(length16):54 """Generate random hex string."""55 return ''.join(random<module 'random' from '/usr/local/lib/python3.12/random.py'>.choices('0123456789abcdef', k=length16))20 passes — pass 1 is the card above pass length1 16 2 16 3 16 4 16 5 16 6 8 7 4 8 4 9 4 ⋯ 9 more passes ⋯ 19 4 20 12 print(f" {random_hex(16)}")
57for _ in range(5):58 print(f" {random_hex(16)}")output cc31667baf165d33print(f" {random_hex(16)}")
57for _ in range(5):58 print(f" {random_hex(16)}")output 7643e7d80fdfed27print(f" {random_hex(16)}")
57for _ in range(5):58 print(f" {random_hex(16)}")output 3606f4c76ff8b24fprint(f" {random_hex(16)}")
57for _ in range(5):58 print(f" {random_hex(16)}")output 98b098d2f129a31eprint(f" {random_hex(16)}")
57for _ in range(5):58 print(f" {random_hex(16)}")output 399698e3b36a45c1print(" Random UUID-like:")
60# Random UUID-like61print("\nRandom UUID-like:")62def random_uuid():output Random UUID-like:for _ in range(3):
pass 1 of 366for _0 in range(3):67 print(f" {random_uuid()}")All 3 passes — pass 1 is the card above pass _1 0 2 1 3 2 print(f" {random_uuid()}")
66for _ in range(3):67 print(f" {random_uuid()}")output ⟨id A⟩print(f" {random_uuid()}")
66for _ in range(3):67 print(f" {random_uuid()}")output ⟨id B⟩print(f" {random_uuid()}")
66for _ in range(3):67 print(f" {random_uuid()}")output ⟨id C⟩print(" Random emails:")
69# Random email70print("\nRandom emails:")71def random_email():output Random emails:for _ in range(5):
pass 1 of 577for _0 in range(5):78 print(f" {random_email()}")All 5 passes — pass 1 is the card above pass _1 0 2 1 3 2 4 3 5 4 username ← kznyczez, domain ← hotmail.com
pass 1 of 570print("\nRandom emails:")71def random_email():72 """Generate random email address."""73 username→ kznyczez = ''.join(random<module 'random' from '/usr/local/lib/python3.12/random.py'>.choices(string.ascii_lowercaseabcdefghijklmnopqrstuvwxyz, k=8))74 domain→ hotmail.com = random<module 'random' from '/usr/local/lib/python3.12/random.py'>.choice(['gmail.com', 'yahoo.com', 'hotmail.com', 'example.com'])75 return f"{usernamekznyczez}@{domainhotmail.com}"All 5 passes — pass 1 is the card above pass usernamedomain1 kznyczez hotmail.com 2 apiulzdx yahoo.com 3 gsaynstr hotmail.com 4 lyxqrdxn example.com 5 isoerjte hotmail.com print(f" {random_email()}")
77for _ in range(5):78 print(f" {random_email()}")output kznyczez@hotmail.comprint(f" {random_email()}")
77for _ in range(5):78 print(f" {random_email()}")output apiulzdx@yahoo.comprint(f" {random_email()}")
77for _ in range(5):78 print(f" {random_email()}")output gsaynstr@hotmail.comprint(f" {random_email()}")
77for _ in range(5):78 print(f" {random_email()}")output lyxqrdxn@example.comprint(f" {random_email()}")
77for _ in range(5):78 print(f" {random_email()}")output isoerjte@hotmail.comprint(" Random phone numbers:")
80# Random phone81print("\nRandom phone numbers:")82def random_phone():output Random phone numbers:for _ in range(5):
pass 1 of 589for _0 in range(5):90 print(f" {random_phone()}")All 5 passes — pass 1 is the card above pass _1 0 2 1 3 2 4 3 5 4 area ← 615, exchange ← 761, number ← 6
pass 1 of 581print("\nRandom phone numbers:")82def random_phone():83 """Generate random phone number."""84 area→ 615 = random<module 'random' from '/usr/local/lib/python3.12/random.py'>.randint(200, 999)85 exchange→ 761 = random<module 'random' from '/usr/local/lib/python3.12/random.py'>.randint(200, 999)86 number→ 6 = random<module 'random' from '/usr/local/lib/python3.12/random.py'>.randint(0, 9999)87 return f"({area615:03d}) {exchange761:03d}-{number6:04d}"All 5 passes — pass 1 is the card above pass areaexchangenumber1 615 761 6 2 511 493 3443 3 640 793 9939 4 870 529 7618 5 652 652 3501 print(f" {random_phone()}")
89for _ in range(5):90 print(f" {random_phone()}")output (615) 761-0006print(f" {random_phone()}")
89for _ in range(5):90 print(f" {random_phone()}")output (511) 493-3443print(f" {random_phone()}")
89for _ in range(5):90 print(f" {random_phone()}")output (640) 793-9939print(f" {random_phone()}")
89for _ in range(5):90 print(f" {random_phone()}")output (870) 529-7618print(f" {random_phone()}")
89for _ in range(5):90 print(f" {random_phone()}")output (652) 652-3501first_names ← ['Alice', 'Bob', 'Charlie', 'Diana', 'Eve', 'Frank']
92# Random name93print("\nRandom names:")94first_names→ ['Alice', 'Bob', 'Charlie', 'Diana', 'Eve', 'Frank'] = ['Alice', 'Bob', 'Charlie', 'Diana', 'Eve', 'Frank']95last_names→ ['Smith', 'Johnson', 'Williams', 'Brown', 'Jones', 'Miller'] = ['Smith', 'Johnson', 'Williams', 'Brown', 'Jones', 'Miller']output Random names:name ← Eve Brown
pass 1 of 597for _0 in range(5):98 name→ Eve Brown = f"{random<module 'random' from '/usr/local/lib/python3.12/random.py'>.choice(first_names['Alice', 'Bob', 'Charlie', 'Diana', 'Eve', 'Frank'])} {random.choice(last_names['Smith', 'Johnson', 'Williams', 'Brown', 'Jones', 'Miller'])}"99 print(f" {nameEve Brown}")output Eve BrownAll 5 passes — pass 1 is the card above pass _name1 0 Eve Brown 2 1 Frank Johnson 3 2 Frank Smith 4 3 Charlie Jones 5 4 Frank Miller print(" Random dates:")
101# Random date102print("\nRandom dates:")103import datetimeoutput Random dates:for _ in range(5):
pass 1 of 5112for _0 in range(5):113 print(f" {random_date()}")All 5 passes — pass 1 is the card above pass _1 0 2 1 3 2 4 3 5 4 year ← 2024, month ← 6, day ← 3
pass 1 of 5105def random_date(start_year2020=2020, end_year2024=2024):106 """Generate random date."""107 year→ 2024 = random<module 'random' from '/usr/local/lib/python3.12/random.py'>.randint(start_year2020, end_year2024)108 month→ 6 = random<module 'random' from '/usr/local/lib/python3.12/random.py'>.randint(1, 12)109 day→ 3 = random<module 'random' from '/usr/local/lib/python3.12/random.py'>.randint(1, 28) # Simplified110 return datetime<module 'datetime' from '/usr/local/lib/python3.12/datetime.py'>.date(year2024, month6, day3)All 5 passes — pass 1 is the card above pass yearmonthday1 2024 6 3 2 2021 11 10 3 2021 4 5 4 2020 1 8 5 2023 10 28 print(f" {random_date()}")
112for _ in range(5):113 print(f" {random_date()}")output 2024-06-03print(f" {random_date()}")
112for _ in range(5):113 print(f" {random_date()}")output 2021-11-10print(f" {random_date()}")
112for _ in range(5):113 print(f" {random_date()}")output 2021-04-05print(f" {random_date()}")
112for _ in range(5):113 print(f" {random_date()}")output 2020-01-08print(f" {random_date()}")
112for _ in range(5):113 print(f" {random_date()}")output 2023-10-28print(" Random RGB colors:")
115# Random color116print("\nRandom RGB colors:")117def random_color():output Random RGB colors:for _ in range(5):
pass 1 of 5124for _0 in range(5):125 print(f" {random_color()}")All 5 passes — pass 1 is the card above pass _1 0 2 1 3 2 4 3 5 4 r ← 37, g ← 233, b ← 212
pass 1 of 5116print("\nRandom RGB colors:")117def random_color():118 """Generate random RGB color."""119 r→ 37 = random<module 'random' from '/usr/local/lib/python3.12/random.py'>.randint(0, 255)120 g→ 233 = random<module 'random' from '/usr/local/lib/python3.12/random.py'>.randint(0, 255)121 b→ 212 = random<module 'random' from '/usr/local/lib/python3.12/random.py'>.randint(0, 255)122 return f"#{r37:02x}{g233:02x}{b212:02x}"All 5 passes — pass 1 is the card above pass rgb1 37 233 212 2 99 196 253 3 204 124 75 4 2 54 217 5 112 90 237 print(f" {random_color()}")
124for _ in range(5):125 print(f" {random_color()}")output #25e9d4print(f" {random_color()}")
124for _ in range(5):125 print(f" {random_color()}")output #63c4fdprint(f" {random_color()}")
124for _ in range(5):125 print(f" {random_color()}")output #cc7c4bprint(f" {random_color()}")
124for _ in range(5):125 print(f" {random_color()}")output #0236d9print(f" {random_color()}")
124for _ in range(5):125 print(f" {random_color()}")output #705aed
Statistical Distributions
Random numbers following specific distributions:
distributions.py
Replay: real traced execution (multi-file project)
# Distributions
import random
import math
random.seed(42)
# Distributions
print("Distributions:")
trials = 50
# Uniform distribution [0, 100)
print("Uniform distribution [0, 100):")
uniform = [0] * 10
for _ in range(trials):
value = random.randint(0, 99)
bin_idx = value // 10
uniform[bin_idx] += 1
for i, count in enumerate(uniform):
bar = '█' * max(1, count // 2)
print(f"[{i*10}-{(i+1)*10}): {count:4d} ({count/trials*100:.1f}%) {bar}")
# Gaussian (normal) distribution
print("\nGaussian distribution (mean=50, stddev=10):")
gaussian = [0] * 10
for _ in range(trials):
value = random.gauss(50, 10)
bin_idx = max(0, min(9, int(value // 10)))
gaussian[bin_idx] += 1
for i, count in enumerate(gaussian):
bar = '█' * max(1, count // 2)
print(f"[{i*10}-{(i+1)*10}): {count:4d} ({count/trials*100:.1f}%) {bar}")
# Exponential distribution
print("\nExponential distribution (lambda=0.1):")
def exponential_random(lambda_param):
"""Generate exponential random variable."""
return -math.log(1 - random.random()) / lambda_param
exponential = [0] * 10
for _ in range(trials):
value = exponential_random(0.1)
bin_idx = min(9, int(value // 10))
exponential[bin_idx] += 1
for i, count in enumerate(exponential):
bar = '█' * max(1, count // 2)
print(f"[{i*10}-{(i+1)*10}): {count:4d} ({count/trials*100:.1f}%) {bar}")
# Binomial distribution (coin flips)
print("\nBinomial distribution (10 flips, p=0.5):")
binomial = [0] * 11
for _ in range(trials):
heads = sum(random.random() < 0.5 for _ in range(10))
binomial[heads] += 1
print("Number of heads:")
for i, count in enumerate(binomial):
print(f"{i:2d}: {count:4d} ({count/trials*100:.1f}%)")
# Triangle distribution
print("\nTriangle distribution [0, 100, mode=50]:")
triangle = [0] * 10
for _ in range(trials):
value = random.triangular(0, 100, 50)
bin_idx = min(9, int(value // 10))
triangle[bin_idx] += 1
for i, count in enumerate(triangle):
bar = '█' * max(1, count // 2)
print(f"[{i*10}-{(i+1)*10}): {count:4d} ({count/trials*100:.1f}%) {bar}")
# Beta distribution (using acceptance-rejection)
print("\nBeta-like distribution:")
def beta_random(alpha, beta):
"""Simple beta random using acceptance-rejection."""
while True:
u = random.random()
v = random.random()
if v <= u**(alpha-1) * (1-u)**(beta-1):
return u
beta_dist = [0] * 10
for _ in range(trials):
value = beta_random(2, 5) * 100
bin_idx = min(9, int(value // 10))
beta_dist[bin_idx] += 1
for i, count in enumerate(beta_dist):
bar = '█' * max(1, count // 2)
print(f"[{i*10}-{(i+1)*10}): {count:4d} ({count/trials*100:.1f}%) {bar}")
# Poisson distribution
print("\nPoisson distribution (lambda=5):")
def poisson_random(lambda_param):
"""Generate Poisson random variable."""
L = math.exp(-lambda_param)
k = 0
p = 1.0
while p > L:
k += 1
p *= random.random()
return k - 1
poisson = [0] * 15
for _ in range(trials):
events = poisson_random(5.0)
if events < len(poisson):
poisson[events] += 1
print("Number of events:")
for i, count in enumerate(poisson[:12]):
print(f"{i:2d}: {count:4d} ({count/trials*100:.1f}%)")
trials ← 50, uniform ← [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
4import math5random<module 'random' from '/usr/local/lib/python3.12/random.py'>.seed(42)67# Distributions8print("Distributions:")9trials→ 50 = 501011# Uniform distribution [0, 100)12print("Uniform distribution [0, 100):")13uniform→ [0, 0, 0, 0, 0, 0, 0, 0, 0, 0] = [0] * 1014for _ in range(trials):outputDistributions:value ← 81, bin_idx ← 8, uniform[bin_idx] ← 1
pass 1 of 5013uniform = [0] * 1014for _0 in range(trials50):15 value→ 81 = random<module 'random' from '/usr/local/lib/python3.12/random.py'>.randint(0, 99)16 bin_idx→ 8 = value81 // 1017 uniform[bin_idx]→ 1 += 150 passes — pass 1 is the card above pass _valuebin_idxuniform[bin_idx]1 0 81 8 0 → 1 2 1 14 1 0 → 1 3 2 3 0 0 → 1 4 3 94 9 0 → 1 5 4 35 3 0 → 1 6 5 31 3 1 → 2 7 6 28 2 0 → 1 8 7 17 1 1 → 2 9 8 94 9 1 → 2 ⋯ 39 more passes ⋯ 49 48 48 4 2 → 3 50 49 12 1 8 → 9 bar ← ██
pass 1 of 1019for i0, count5 in enumerate(uniform[5, 9, 7, 4, 3, 4, 3, 4, 5, 6]):20 bar→ ██ = '█' * max(1, count5 // 2)21 print(f"[{i0*10}-{(i+1)*10}): {count5:4d} ({count/trials50*100:.1f}%) {bar██}")All 10 passes — pass 1 is the card above pass icountbar1 0 5 ██ 2 1 9 ████ 3 2 7 ███ 4 3 4 ██ 5 4 3 █ 6 5 4 ██ 7 6 3 █ 8 7 4 ██ 9 8 5 ██ 10 9 6 ███ gaussian ← [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
23# Gaussian (normal) distribution24print("\nGaussian distribution (mean=50, stddev=10):")25gaussian→ [0, 0, 0, 0, 0, 0, 0, 0, 0, 0] = [0] * 1026for _ in range(trials):output Gaussian distribution (mean=50, stddev=10):value ← 44.19279044520348, bin_idx ← 4, gaussian[bin_idx] ← 1
pass 1 of 5025gaussian = [0] * 1026for _0 in range(trials50):27 value→ 44.19279044520348 = random<module 'random' from '/usr/local/lib/python3.12/random.py'>.gauss(50, 10)28 bin_idx→ 4 = max(0, min(9, int(value44.19279044520348 // 10)))29 gaussian[bin_idx]→ 1 += 150 passes — pass 1 is the card above pass _valuebin_idxgaussian[bin_idx]1 0 44.19279044520348 4 0 → 1 2 1 57.11208242398997 5 0 → 1 3 2 49.72842667783163 4 1 → 2 4 3 52.96829648952292 5 1 → 2 5 4 45.002942477363675 4 2 → 3 6 5 51.30229197259388 5 2 → 3 7 6 53.57824641259233 5 3 → 4 8 7 48.099521685907 4 3 → 4 9 8 46.22805825438125 4 4 → 5 ⋯ 39 more passes ⋯ 49 48 60.74513921807913 6 6 → 7 50 49 40.488800603795404 4 20 → 21 bar ← █
pass 1 of 1031for i0, count0 in enumerate(gaussian[0, 0, 0, 4, 21, 16, 7, 2, 0, 0]):32 bar→ █ = '█' * max(1, count0 // 2)33 print(f"[{i0*10}-{(i+1)*10}): {count0:4d} ({count/trials50*100:.1f}%) {bar█}")All 10 passes — pass 1 is the card above pass icountbar1 0 0 █ 2 1 0 █ 3 2 0 █ 4 3 4 ██ 5 4 21 ██████████ 6 5 16 ████████ 7 6 7 ███ 8 7 2 █ 9 8 0 █ 10 9 0 █ exponential ← [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
35# Exponential distribution36print("\nExponential distribution (lambda=0.1):")37def exponential_random(lambda_param):38 """Generate exponential random variable."""39 return -math.log(1 - random.random()) / lambda_param4041exponential→ [0, 0, 0, 0, 0, 0, 0, 0, 0, 0] = [0] * 1042for _ in range(trials):output Exponential distribution (lambda=0.1):for _ in range(trials):
pass 1 of 5041exponential = [0] * 1042for _0 in range(trials50):43 value = exponential_random(0.1)44 bin_idx = min(9, int(value // 10))50 passes — pass 1 is the card above pass _1 0 2 1 3 2 4 3 5 4 6 5 7 6 8 7 9 8 ⋯ 39 more passes ⋯ 49 48 50 49 def exponential_random(lambda_param):
pass 1 of 5036print("\nExponential distribution (lambda=0.1):")37def exponential_random(lambda_param0.1):38 """Generate exponential random variable."""39 return -math<module 'math' from '/usr/local/lib/python3.12/lib-dynload/math.cpython-312-x86_64-linux-gnu.so'>.log(1 - random<module 'random' from '/usr/local/lib/python3.12/random.py'>.random()) / lambda_param0.1value ← 1.5416753548505386, bin_idx ← 0, exponential[bin_idx] ← 1
42for _ in range(trials):43 value→ 1.5416753548505386 = exponential_random(0.1)44 bin_idx→ 0 = min(9, int(value1.5416753548505386 // 10))45 exponential[bin_idx]→ 1 += 1value ← 1.5039312108150928, bin_idx ← 0, exponential[bin_idx] ← 2
42for _ in range(trials):43 value→ 1.5039312108150928 = exponential_random(0.1)44 bin_idx→ 0 = min(9, int(value1.5039312108150928 // 10))45 exponential[bin_idx]→ 2 += 1value ← 13.664485272732316, bin_idx ← 1, exponential[bin_idx] ← 1
42for _ in range(trials):43 value→ 13.664485272732316 = exponential_random(0.1)44 bin_idx→ 1 = min(9, int(value13.664485272732316 // 10))45 exponential[bin_idx]→ 1 += 1value ← 7.743079740042821, bin_idx ← 0, exponential[bin_idx] ← 3
42for _ in range(trials):43 value→ 7.743079740042821 = exponential_random(0.1)44 bin_idx→ 0 = min(9, int(value7.743079740042821 // 10))45 exponential[bin_idx]→ 3 += 1value ← 13.744203832556883, bin_idx ← 1, exponential[bin_idx] ← 2
42for _ in range(trials):43 value→ 13.744203832556883 = exponential_random(0.1)44 bin_idx→ 1 = min(9, int(value13.744203832556883 // 10))45 exponential[bin_idx]→ 2 += 1value ← 5.593750146891416, bin_idx ← 0, exponential[bin_idx] ← 4
42for _ in range(trials):43 value→ 5.593750146891416 = exponential_random(0.1)44 bin_idx→ 0 = min(9, int(value5.593750146891416 // 10))45 exponential[bin_idx]→ 4 += 1value ← 8.759478719901445, bin_idx ← 0, exponential[bin_idx] ← 5
42for _ in range(trials):43 value→ 8.759478719901445 = exponential_random(0.1)44 bin_idx→ 0 = min(9, int(value8.759478719901445 // 10))45 exponential[bin_idx]→ 5 += 1value ← 4.494114215362852, bin_idx ← 0, exponential[bin_idx] ← 6
42for _ in range(trials):43 value→ 4.494114215362852 = exponential_random(0.1)44 bin_idx→ 0 = min(9, int(value4.494114215362852 // 10))45 exponential[bin_idx]→ 6 += 1value ← 59.24096778005002, bin_idx ← 5, exponential[bin_idx] ← 1
42for _ in range(trials):43 value→ 59.24096778005002 = exponential_random(0.1)44 bin_idx→ 5 = min(9, int(value59.24096778005002 // 10))45 exponential[bin_idx]→ 1 += 1value ← 1.488849372558642, bin_idx ← 0, exponential[bin_idx] ← 7
42for _ in range(trials):43 value→ 1.488849372558642 = exponential_random(0.1)44 bin_idx→ 0 = min(9, int(value1.488849372558642 // 10))45 exponential[bin_idx]→ 7 += 1value ← 6.802622191377466, bin_idx ← 0, exponential[bin_idx] ← 8
42for _ in range(trials):43 value→ 6.802622191377466 = exponential_random(0.1)44 bin_idx→ 0 = min(9, int(value6.802622191377466 // 10))45 exponential[bin_idx]→ 8 += 1value ← 14.096946550022391, bin_idx ← 1, exponential[bin_idx] ← 3
42for _ in range(trials):43 value→ 14.096946550022391 = exponential_random(0.1)44 bin_idx→ 1 = min(9, int(value14.096946550022391 // 10))45 exponential[bin_idx]→ 3 += 1value ← 19.740219773654097, bin_idx ← 1, exponential[bin_idx] ← 4
42for _ in range(trials):43 value→ 19.740219773654097 = exponential_random(0.1)44 bin_idx→ 1 = min(9, int(value19.740219773654097 // 10))45 exponential[bin_idx]→ 4 += 1value ← 1.6586725754184823, bin_idx ← 0, exponential[bin_idx] ← 9
42for _ in range(trials):43 value→ 1.6586725754184823 = exponential_random(0.1)44 bin_idx→ 0 = min(9, int(value1.6586725754184823 // 10))45 exponential[bin_idx]→ 9 += 1value ← 1.743319608534298, bin_idx ← 0, exponential[bin_idx] ← 10
42for _ in range(trials):43 value→ 1.743319608534298 = exponential_random(0.1)44 bin_idx→ 0 = min(9, int(value1.743319608534298 // 10))45 exponential[bin_idx]→ 10 += 1value ← 11.409392071946751, bin_idx ← 1, exponential[bin_idx] ← 5
42for _ in range(trials):43 value→ 11.409392071946751 = exponential_random(0.1)44 bin_idx→ 1 = min(9, int(value11.409392071946751 // 10))45 exponential[bin_idx]→ 5 += 1value ← 9.073533662038939, bin_idx ← 0, exponential[bin_idx] ← 11
42for _ in range(trials):43 value→ 9.073533662038939 = exponential_random(0.1)44 bin_idx→ 0 = min(9, int(value9.073533662038939 // 10))45 exponential[bin_idx]→ 11 += 1value ← 4.857547277330106, bin_idx ← 0, exponential[bin_idx] ← 12
42for _ in range(trials):43 value→ 4.857547277330106 = exponential_random(0.1)44 bin_idx→ 0 = min(9, int(value4.857547277330106 // 10))45 exponential[bin_idx]→ 12 += 1value ← 9.0606427172524, bin_idx ← 0, exponential[bin_idx] ← 13
42for _ in range(trials):43 value→ 9.0606427172524 = exponential_random(0.1)44 bin_idx→ 0 = min(9, int(value9.0606427172524 // 10))45 exponential[bin_idx]→ 13 += 1value ← 6.312055680689002, bin_idx ← 0, exponential[bin_idx] ← 14
42for _ in range(trials):43 value→ 6.312055680689002 = exponential_random(0.1)44 bin_idx→ 0 = min(9, int(value6.312055680689002 // 10))45 exponential[bin_idx]→ 14 += 1value ← 2.895693455409019, bin_idx ← 0, exponential[bin_idx] ← 15
42for _ in range(trials):43 value→ 2.895693455409019 = exponential_random(0.1)44 bin_idx→ 0 = min(9, int(value2.895693455409019 // 10))45 exponential[bin_idx]→ 15 += 1value ← 8.057021727669028, bin_idx ← 0, exponential[bin_idx] ← 16
42for _ in range(trials):43 value→ 8.057021727669028 = exponential_random(0.1)44 bin_idx→ 0 = min(9, int(value8.057021727669028 // 10))45 exponential[bin_idx]→ 16 += 1value ← 28.547686427098082, bin_idx ← 2, exponential[bin_idx] ← 1
42for _ in range(trials):43 value→ 28.547686427098082 = exponential_random(0.1)44 bin_idx→ 2 = min(9, int(value28.547686427098082 // 10))45 exponential[bin_idx]→ 1 += 1value ← 11.403203326659119, bin_idx ← 1, exponential[bin_idx] ← 6
42for _ in range(trials):43 value→ 11.403203326659119 = exponential_random(0.1)44 bin_idx→ 1 = min(9, int(value11.403203326659119 // 10))45 exponential[bin_idx]→ 6 += 1value ← 1.2166125710144062, bin_idx ← 0, exponential[bin_idx] ← 17
42for _ in range(trials):43 value→ 1.2166125710144062 = exponential_random(0.1)44 bin_idx→ 0 = min(9, int(value1.2166125710144062 // 10))45 exponential[bin_idx]→ 17 += 1value ← 21.609852764500445, bin_idx ← 2, exponential[bin_idx] ← 2
42for _ in range(trials):43 value→ 21.609852764500445 = exponential_random(0.1)44 bin_idx→ 2 = min(9, int(value21.609852764500445 // 10))45 exponential[bin_idx]→ 2 += 1value ← 13.89811738210705, bin_idx ← 1, exponential[bin_idx] ← 7
42for _ in range(trials):43 value→ 13.89811738210705 = exponential_random(0.1)44 bin_idx→ 1 = min(9, int(value13.89811738210705 // 10))45 exponential[bin_idx]→ 7 += 1value ← 14.636019873898748, bin_idx ← 1, exponential[bin_idx] ← 8
42for _ in range(trials):43 value→ 14.636019873898748 = exponential_random(0.1)44 bin_idx→ 1 = min(9, int(value14.636019873898748 // 10))45 exponential[bin_idx]→ 8 += 1value ← 4.157813475397454, bin_idx ← 0, exponential[bin_idx] ← 18
42for _ in range(trials):43 value→ 4.157813475397454 = exponential_random(0.1)44 bin_idx→ 0 = min(9, int(value4.157813475397454 // 10))45 exponential[bin_idx]→ 18 += 1value ← 3.4743225894876204, bin_idx ← 0, exponential[bin_idx] ← 19
42for _ in range(trials):43 value→ 3.4743225894876204 = exponential_random(0.1)44 bin_idx→ 0 = min(9, int(value3.4743225894876204 // 10))45 exponential[bin_idx]→ 19 += 1value ← 1.7216263899715338, bin_idx ← 0, exponential[bin_idx] ← 20
42for _ in range(trials):43 value→ 1.7216263899715338 = exponential_random(0.1)44 bin_idx→ 0 = min(9, int(value1.7216263899715338 // 10))45 exponential[bin_idx]→ 20 += 1value ← 0.03250593561138638, bin_idx ← 0, exponential[bin_idx] ← 21
42for _ in range(trials):43 value→ 0.03250593561138638 = exponential_random(0.1)44 bin_idx→ 0 = min(9, int(value0.03250593561138638 // 10))45 exponential[bin_idx]→ 21 += 1value ← 12.804653759267332, bin_idx ← 1, exponential[bin_idx] ← 9
42for _ in range(trials):43 value→ 12.804653759267332 = exponential_random(0.1)44 bin_idx→ 1 = min(9, int(value12.804653759267332 // 10))45 exponential[bin_idx]→ 9 += 1value ← 12.718258573882279, bin_idx ← 1, exponential[bin_idx] ← 10
42for _ in range(trials):43 value→ 12.718258573882279 = exponential_random(0.1)44 bin_idx→ 1 = min(9, int(value12.718258573882279 // 10))45 exponential[bin_idx]→ 10 += 1value ← 35.71562264551061, bin_idx ← 3, exponential[bin_idx] ← 1
42for _ in range(trials):43 value→ 35.71562264551061 = exponential_random(0.1)44 bin_idx→ 3 = min(9, int(value35.71562264551061 // 10))45 exponential[bin_idx]→ 1 += 1value ← 14.353257904100829, bin_idx ← 1, exponential[bin_idx] ← 11
42for _ in range(trials):43 value→ 14.353257904100829 = exponential_random(0.1)44 bin_idx→ 1 = min(9, int(value14.353257904100829 // 10))45 exponential[bin_idx]→ 11 += 1value ← 7.086297688691896, bin_idx ← 0, exponential[bin_idx] ← 22
42for _ in range(trials):43 value→ 7.086297688691896 = exponential_random(0.1)44 bin_idx→ 0 = min(9, int(value7.086297688691896 // 10))45 exponential[bin_idx]→ 22 += 1value ← 1.1250914275718797, bin_idx ← 0, exponential[bin_idx] ← 23
42for _ in range(trials):43 value→ 1.1250914275718797 = exponential_random(0.1)44 bin_idx→ 0 = min(9, int(value1.1250914275718797 // 10))45 exponential[bin_idx]→ 23 += 1value ← 9.816300897730144, bin_idx ← 0, exponential[bin_idx] ← 24
42for _ in range(trials):43 value→ 9.816300897730144 = exponential_random(0.1)44 bin_idx→ 0 = min(9, int(value9.816300897730144 // 10))45 exponential[bin_idx]→ 24 += 1value ← 18.43069699585126, bin_idx ← 1, exponential[bin_idx] ← 12
42for _ in range(trials):43 value→ 18.43069699585126 = exponential_random(0.1)44 bin_idx→ 1 = min(9, int(value18.43069699585126 // 10))45 exponential[bin_idx]→ 12 += 1value ← 7.08591597899194, bin_idx ← 0, exponential[bin_idx] ← 25
42for _ in range(trials):43 value→ 7.08591597899194 = exponential_random(0.1)44 bin_idx→ 0 = min(9, int(value7.08591597899194 // 10))45 exponential[bin_idx]→ 25 += 1value ← 2.217833540080497, bin_idx ← 0, exponential[bin_idx] ← 26
42for _ in range(trials):43 value→ 2.217833540080497 = exponential_random(0.1)44 bin_idx→ 0 = min(9, int(value2.217833540080497 // 10))45 exponential[bin_idx]→ 26 += 1value ← 4.682673016811133, bin_idx ← 0, exponential[bin_idx] ← 27
42for _ in range(trials):43 value→ 4.682673016811133 = exponential_random(0.1)44 bin_idx→ 0 = min(9, int(value4.682673016811133 // 10))45 exponential[bin_idx]→ 27 += 1value ← 1.7619917975467874, bin_idx ← 0, exponential[bin_idx] ← 28
42for _ in range(trials):43 value→ 1.7619917975467874 = exponential_random(0.1)44 bin_idx→ 0 = min(9, int(value1.7619917975467874 // 10))45 exponential[bin_idx]→ 28 += 1value ← 30.682975395971592, bin_idx ← 3, exponential[bin_idx] ← 2
42for _ in range(trials):43 value→ 30.682975395971592 = exponential_random(0.1)44 bin_idx→ 3 = min(9, int(value30.682975395971592 // 10))45 exponential[bin_idx]→ 2 += 1value ← 25.56627131487352, bin_idx ← 2, exponential[bin_idx] ← 3
42for _ in range(trials):43 value→ 25.56627131487352 = exponential_random(0.1)44 bin_idx→ 2 = min(9, int(value25.56627131487352 // 10))45 exponential[bin_idx]→ 3 += 1value ← 25.07072438555419, bin_idx ← 2, exponential[bin_idx] ← 4
42for _ in range(trials):43 value→ 25.07072438555419 = exponential_random(0.1)44 bin_idx→ 2 = min(9, int(value25.07072438555419 // 10))45 exponential[bin_idx]→ 4 += 1value ← 9.13655667857379, bin_idx ← 0, exponential[bin_idx] ← 29
42for _ in range(trials):43 value→ 9.13655667857379 = exponential_random(0.1)44 bin_idx→ 0 = min(9, int(value9.13655667857379 // 10))45 exponential[bin_idx]→ 29 += 1value ← 6.7061437776135, bin_idx ← 0, exponential[bin_idx] ← 30
42for _ in range(trials):43 value→ 6.7061437776135 = exponential_random(0.1)44 bin_idx→ 0 = min(9, int(value6.7061437776135 // 10))45 exponential[bin_idx]→ 30 += 1value ← 1.1863458911113907, bin_idx ← 0, exponential[bin_idx] ← 31
42for _ in range(trials):43 value→ 1.1863458911113907 = exponential_random(0.1)44 bin_idx→ 0 = min(9, int(value1.1863458911113907 // 10))45 exponential[bin_idx]→ 31 += 1bar ← ███████████████
pass 1 of 1047for i0, count31 in enumerate(exponential[31, 12, 4, 2, 0, 1, 0, 0, 0, 0]):48 bar→ ███████████████ = '█' * max(1, count31 // 2)49 print(f"[{i0*10}-{(i+1)*10}): {count31:4d} ({count/trials50*100:.1f}%) {bar███████████████}")All 10 passes — pass 1 is the card above pass icountbar1 0 31 ███████████████ 2 1 12 ██████ 3 2 4 ██ 4 3 2 █ 5 4 0 █ 6 5 1 █ 7 6 0 █ 8 7 0 █ 9 8 0 █ 10 9 0 █ binomial ← [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
51# Binomial distribution (coin flips)52print("\nBinomial distribution (10 flips, p=0.5):")53binomial→ [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] = [0] * 1154for _ in range(trials):output Binomial distribution (10 flips, p=0.5):heads ← 4, binomial[heads] ← 1
pass 1 of 5053binomial = [0] * 1154for _0 in range(trials50):55 heads→ 4 = sum(random<module 'random' from '/usr/local/lib/python3.12/random.py'>.random() < 0.5 for _ in range(10))56 binomial[heads]→ 1 += 150 passes — pass 1 is the card above pass _headsbinomial[heads]1 0 4 0 → 1 2 1 2 0 → 1 3 2 6 0 → 1 4 3 6 1 → 2 5 4 5 0 → 1 6 5 5 1 → 2 7 6 6 2 → 3 8 7 5 2 → 3 9 8 4 1 → 2 ⋯ 39 more passes ⋯ 49 48 6 9 → 10 50 49 6 10 → 11 print("Number of heads:")
58print("Number of heads:")59for i, count in enumerate(binomial):outputNumber of heads:for i, count in enumerate(binomial):
pass 1 of 1158print("Number of heads:")59for i0, count0 in enumerate(binomial[0, 0, 4, 6, 9, 11, 11, 7, 2, 0, 0]):60 print(f"{i0:2d}: {count0:4d} ({count/trials50*100:.1f}%)")output 0: 0 (0.0%)All 11 passes — pass 1 is the card above pass icount1 0 0 2 1 0 3 2 4 4 3 6 5 4 9 6 5 11 7 6 11 8 7 7 9 8 2 10 9 0 11 10 0 triangle ← [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
62# Triangle distribution63print("\nTriangle distribution [0, 100, mode=50]:")64triangle→ [0, 0, 0, 0, 0, 0, 0, 0, 0, 0] = [0] * 1065for _ in range(trials):output Triangle distribution [0, 100, mode=50]:value ← 63.84786683395782, bin_idx ← 6, triangle[bin_idx] ← 1
pass 1 of 5064triangle = [0] * 1065for _0 in range(trials50):66 value→ 63.84786683395782 = random<module 'random' from '/usr/local/lib/python3.12/random.py'>.triangular(0, 100, 50)67 bin_idx→ 6 = min(9, int(value63.84786683395782 // 10))68 triangle[bin_idx]→ 1 += 150 passes — pass 1 is the card above pass _valuebin_idxtriangle[bin_idx]1 0 63.84786683395782 6 0 → 1 2 1 37.181959803693054 3 0 → 1 3 2 54.70027074919713 5 0 → 1 4 3 65.37399065536736 6 1 → 2 5 4 55.09959579871812 5 1 → 2 6 5 90.12848300981888 9 0 → 1 7 6 71.07149877085472 7 0 → 1 8 7 38.48678105954981 3 1 → 2 9 8 42.478292641617934 4 0 → 1 ⋯ 39 more passes ⋯ 49 48 85.87710678265589 8 3 → 4 50 49 22.274826012203576 2 5 → 6 bar ← █
pass 1 of 1070for i0, count1 in enumerate(triangle[1, 4, 6, 8, 7, 9, 7, 3, 4, 1]):71 bar→ █ = '█' * max(1, count1 // 2)72 print(f"[{i0*10}-{(i+1)*10}): {count1:4d} ({count/trials50*100:.1f}%) {bar█}")All 10 passes — pass 1 is the card above pass icountbar1 0 1 █ 2 1 4 ██ 3 2 6 ███ 4 3 8 ████ 5 4 7 ███ 6 5 9 ████ 7 6 7 ███ 8 7 3 █ 9 8 4 ██ 10 9 1 █ beta_dist ← [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
74# Beta distribution (using acceptance-rejection)75print("\nBeta-like distribution:")76def beta_random(alpha, beta):77 """Simple beta random using acceptance-rejection."""78 while True:79 u = random.random()80 v = random.random()81 if v <= u**(alpha-1) * (1-u)**(beta-1):82 return u8384beta_dist→ [0, 0, 0, 0, 0, 0, 0, 0, 0, 0] = [0] * 1085for _ in range(trials):output Beta-like distribution:for _ in range(trials):
pass 1 of 5084beta_dist = [0] * 1085for _0 in range(trials50):86 value = beta_random(2, 5) * 10087 bin_idx = min(9, int(value // 10))50 passes — pass 1 is the card above pass _1 0 2 1 3 2 4 3 5 4 6 5 7 6 8 7 9 8 ⋯ 39 more passes ⋯ 49 48 50 49 def beta_random(alpha, beta):
pass 1 of 5075print("\nBeta-like distribution:")76def beta_random(alpha2, beta5):77 """Simple beta random using acceptance-rejection."""78 while True:u ← 0.7627371043547087, v ← 0.6255150304066032
pass 1 of 175277"""Simple beta random using acceptance-rejection."""78while True:79 u→ 0.7627371043547087 = random<module 'random' from '/usr/local/lib/python3.12/random.py'>.random()80 v→ 0.6255150304066032 = random<module 'random' from '/usr/local/lib/python3.12/random.py'>.random()81 if v <= u**(alpha-1) * (1-u)**(beta-1):1752 passes — pass 1 is the card above pass uv1 0.7627371043547087 0.6255150304066032 2 0.2646461252900074 0.0811874654669843 3 0.2398651776746924 0.551992245577759 4 0.15653261544313812 0.40851757266002664 5 0.6893711357847689 0.46991247900768585 6 0.03263985538092429 0.2881048895936893 7 0.28271837311228776 0.8596067313405225 8 0.07117976015347927 0.23342380616002556 9 0.26455462565889964 0.7912477450449615 ⋯ 1741 more passes ⋯ 1751 0.0784875726387807 0.9557796840624815 1752 0.49266486327221226 0.006623676152856639 if v <= u**(alpha-1) * (1-u)**(beta-1):
pass 1 of 5080v = random.random()81if v0.02886944244166323 <= u0.4847117969499981**(alpha2-1) * (1-u)**(beta5-1):82 return u0.484711796949998150 passes — pass 1 is the card above pass vu1 0.02886944244166323 0.4847117969499981 2 0.008097965761490244 0.39580574091136733 3 0.045346811013382116 0.11667593672705445 4 0.06355803814713623 0.11364930678654661 5 0.01823053779153272 0.5084884864228076 6 0.03830319111704372 0.14604930682182005 7 0.001564335933398997 0.29127198777525787 8 0.07405203917654368 0.13572841773052724 9 0.050605291800290275 0.1408776350081835 ⋯ 39 more passes ⋯ 49 0.015366522497873958 0.20031038312422256 50 0.006623676152856639 0.49266486327221226 value ← 48.47117969499981, bin_idx ← 4, beta_dist[bin_idx] ← 1
85for _ in range(trials):86 value→ 48.47117969499981 = beta_random(2, 5) * 10087 bin_idx→ 4 = min(9, int(value48.47117969499981 // 10))88 beta_dist[bin_idx]→ 1 += 1value ← 39.58057409113673, bin_idx ← 3, beta_dist[bin_idx] ← 1
85for _ in range(trials):86 value→ 39.58057409113673 = beta_random(2, 5) * 10087 bin_idx→ 3 = min(9, int(value39.58057409113673 // 10))88 beta_dist[bin_idx]→ 1 += 1value ← 11.667593672705445, bin_idx ← 1, beta_dist[bin_idx] ← 1
85for _ in range(trials):86 value→ 11.667593672705445 = beta_random(2, 5) * 10087 bin_idx→ 1 = min(9, int(value11.667593672705445 // 10))88 beta_dist[bin_idx]→ 1 += 1value ← 11.36493067865466, bin_idx ← 1, beta_dist[bin_idx] ← 2
85for _ in range(trials):86 value→ 11.36493067865466 = beta_random(2, 5) * 10087 bin_idx→ 1 = min(9, int(value11.36493067865466 // 10))88 beta_dist[bin_idx]→ 2 += 1value ← 50.848848642280764, bin_idx ← 5, beta_dist[bin_idx] ← 1
85for _ in range(trials):86 value→ 50.848848642280764 = beta_random(2, 5) * 10087 bin_idx→ 5 = min(9, int(value50.848848642280764 // 10))88 beta_dist[bin_idx]→ 1 += 1value ← 14.604930682182005, bin_idx ← 1, beta_dist[bin_idx] ← 3
85for _ in range(trials):86 value→ 14.604930682182005 = beta_random(2, 5) * 10087 bin_idx→ 1 = min(9, int(value14.604930682182005 // 10))88 beta_dist[bin_idx]→ 3 += 1value ← 29.127198777525788, bin_idx ← 2, beta_dist[bin_idx] ← 1
85for _ in range(trials):86 value→ 29.127198777525788 = beta_random(2, 5) * 10087 bin_idx→ 2 = min(9, int(value29.127198777525788 // 10))88 beta_dist[bin_idx]→ 1 += 1value ← 13.572841773052724, bin_idx ← 1, beta_dist[bin_idx] ← 4
85for _ in range(trials):86 value→ 13.572841773052724 = beta_random(2, 5) * 10087 bin_idx→ 1 = min(9, int(value13.572841773052724 // 10))88 beta_dist[bin_idx]→ 4 += 1value ← 14.087763500818351, bin_idx ← 1, beta_dist[bin_idx] ← 5
85for _ in range(trials):86 value→ 14.087763500818351 = beta_random(2, 5) * 10087 bin_idx→ 1 = min(9, int(value14.087763500818351 // 10))88 beta_dist[bin_idx]→ 5 += 1value ← 28.574838144101754, bin_idx ← 2, beta_dist[bin_idx] ← 2
85for _ in range(trials):86 value→ 28.574838144101754 = beta_random(2, 5) * 10087 bin_idx→ 2 = min(9, int(value28.574838144101754 // 10))88 beta_dist[bin_idx]→ 2 += 1value ← 40.325407211435845, bin_idx ← 4, beta_dist[bin_idx] ← 2
85for _ in range(trials):86 value→ 40.325407211435845 = beta_random(2, 5) * 10087 bin_idx→ 4 = min(9, int(value40.325407211435845 // 10))88 beta_dist[bin_idx]→ 2 += 1value ← 40.13870842760645, bin_idx ← 4, beta_dist[bin_idx] ← 3
85for _ in range(trials):86 value→ 40.13870842760645 = beta_random(2, 5) * 10087 bin_idx→ 4 = min(9, int(value40.13870842760645 // 10))88 beta_dist[bin_idx]→ 3 += 1value ← 14.647612192073945, bin_idx ← 1, beta_dist[bin_idx] ← 6
85for _ in range(trials):86 value→ 14.647612192073945 = beta_random(2, 5) * 10087 bin_idx→ 1 = min(9, int(value14.647612192073945 // 10))88 beta_dist[bin_idx]→ 6 += 1value ← 23.928206594629607, bin_idx ← 2, beta_dist[bin_idx] ← 3
85for _ in range(trials):86 value→ 23.928206594629607 = beta_random(2, 5) * 10087 bin_idx→ 2 = min(9, int(value23.928206594629607 // 10))88 beta_dist[bin_idx]→ 3 += 1value ← 31.2922018870191, bin_idx ← 3, beta_dist[bin_idx] ← 2
85for _ in range(trials):86 value→ 31.2922018870191 = beta_random(2, 5) * 10087 bin_idx→ 3 = min(9, int(value31.2922018870191 // 10))88 beta_dist[bin_idx]→ 2 += 1value ← 41.133850821741646, bin_idx ← 4, beta_dist[bin_idx] ← 4
85for _ in range(trials):86 value→ 41.133850821741646 = beta_random(2, 5) * 10087 bin_idx→ 4 = min(9, int(value41.133850821741646 // 10))88 beta_dist[bin_idx]→ 4 += 1value ← 41.83006923172265, bin_idx ← 4, beta_dist[bin_idx] ← 5
85for _ in range(trials):86 value→ 41.83006923172265 = beta_random(2, 5) * 10087 bin_idx→ 4 = min(9, int(value41.83006923172265 // 10))88 beta_dist[bin_idx]→ 5 += 1value ← 21.342117694621308, bin_idx ← 2, beta_dist[bin_idx] ← 4
85for _ in range(trials):86 value→ 21.342117694621308 = beta_random(2, 5) * 10087 bin_idx→ 2 = min(9, int(value21.342117694621308 // 10))88 beta_dist[bin_idx]→ 4 += 1value ← 44.50668767786543, bin_idx ← 4, beta_dist[bin_idx] ← 6
85for _ in range(trials):86 value→ 44.50668767786543 = beta_random(2, 5) * 10087 bin_idx→ 4 = min(9, int(value44.50668767786543 // 10))88 beta_dist[bin_idx]→ 6 += 1value ← 58.73634969259245, bin_idx ← 5, beta_dist[bin_idx] ← 2
85for _ in range(trials):86 value→ 58.73634969259245 = beta_random(2, 5) * 10087 bin_idx→ 5 = min(9, int(value58.73634969259245 // 10))88 beta_dist[bin_idx]→ 2 += 1value ← 74.07799624564117, bin_idx ← 7, beta_dist[bin_idx] ← 1
85for _ in range(trials):86 value→ 74.07799624564117 = beta_random(2, 5) * 10087 bin_idx→ 7 = min(9, int(value74.07799624564117 // 10))88 beta_dist[bin_idx]→ 1 += 1value ← 27.98252708690363, bin_idx ← 2, beta_dist[bin_idx] ← 5
85for _ in range(trials):86 value→ 27.98252708690363 = beta_random(2, 5) * 10087 bin_idx→ 2 = min(9, int(value27.98252708690363 // 10))88 beta_dist[bin_idx]→ 5 += 1value ← 27.043568476897384, bin_idx ← 2, beta_dist[bin_idx] ← 6
85for _ in range(trials):86 value→ 27.043568476897384 = beta_random(2, 5) * 10087 bin_idx→ 2 = min(9, int(value27.043568476897384 // 10))88 beta_dist[bin_idx]→ 6 += 1value ← 33.57494655745471, bin_idx ← 3, beta_dist[bin_idx] ← 3
85for _ in range(trials):86 value→ 33.57494655745471 = beta_random(2, 5) * 10087 bin_idx→ 3 = min(9, int(value33.57494655745471 // 10))88 beta_dist[bin_idx]→ 3 += 1value ← 3.3412786535237826, bin_idx ← 0, beta_dist[bin_idx] ← 1
85for _ in range(trials):86 value→ 3.3412786535237826 = beta_random(2, 5) * 10087 bin_idx→ 0 = min(9, int(value3.3412786535237826 // 10))88 beta_dist[bin_idx]→ 1 += 1value ← 16.09044949113211, bin_idx ← 1, beta_dist[bin_idx] ← 7
85for _ in range(trials):86 value→ 16.09044949113211 = beta_random(2, 5) * 10087 bin_idx→ 1 = min(9, int(value16.09044949113211 // 10))88 beta_dist[bin_idx]→ 7 += 1value ← 22.751275862456787, bin_idx ← 2, beta_dist[bin_idx] ← 7
85for _ in range(trials):86 value→ 22.751275862456787 = beta_random(2, 5) * 10087 bin_idx→ 2 = min(9, int(value22.751275862456787 // 10))88 beta_dist[bin_idx]→ 7 += 1value ← 37.39102096931441, bin_idx ← 3, beta_dist[bin_idx] ← 4
85for _ in range(trials):86 value→ 37.39102096931441 = beta_random(2, 5) * 10087 bin_idx→ 3 = min(9, int(value37.39102096931441 // 10))88 beta_dist[bin_idx]→ 4 += 1value ← 5.7627515863554635, bin_idx ← 0, beta_dist[bin_idx] ← 2
85for _ in range(trials):86 value→ 5.7627515863554635 = beta_random(2, 5) * 10087 bin_idx→ 0 = min(9, int(value5.7627515863554635 // 10))88 beta_dist[bin_idx]→ 2 += 1value ← 9.039192114077043, bin_idx ← 0, beta_dist[bin_idx] ← 3
85for _ in range(trials):86 value→ 9.039192114077043 = beta_random(2, 5) * 10087 bin_idx→ 0 = min(9, int(value9.039192114077043 // 10))88 beta_dist[bin_idx]→ 3 += 1value ← 24.030207767849443, bin_idx ← 2, beta_dist[bin_idx] ← 8
85for _ in range(trials):86 value→ 24.030207767849443 = beta_random(2, 5) * 10087 bin_idx→ 2 = min(9, int(value24.030207767849443 // 10))88 beta_dist[bin_idx]→ 8 += 1value ← 13.992950857641695, bin_idx ← 1, beta_dist[bin_idx] ← 8
85for _ in range(trials):86 value→ 13.992950857641695 = beta_random(2, 5) * 10087 bin_idx→ 1 = min(9, int(value13.992950857641695 // 10))88 beta_dist[bin_idx]→ 8 += 1value ← 11.778147124153794, bin_idx ← 1, beta_dist[bin_idx] ← 9
85for _ in range(trials):86 value→ 11.778147124153794 = beta_random(2, 5) * 10087 bin_idx→ 1 = min(9, int(value11.778147124153794 // 10))88 beta_dist[bin_idx]→ 9 += 1value ← 18.035676910684384, bin_idx ← 1, beta_dist[bin_idx] ← 10
85for _ in range(trials):86 value→ 18.035676910684384 = beta_random(2, 5) * 10087 bin_idx→ 1 = min(9, int(value18.035676910684384 // 10))88 beta_dist[bin_idx]→ 10 += 1value ← 31.34140717573387, bin_idx ← 3, beta_dist[bin_idx] ← 5
85for _ in range(trials):86 value→ 31.34140717573387 = beta_random(2, 5) * 10087 bin_idx→ 3 = min(9, int(value31.34140717573387 // 10))88 beta_dist[bin_idx]→ 5 += 1value ← 53.10337855709844, bin_idx ← 5, beta_dist[bin_idx] ← 3
85for _ in range(trials):86 value→ 53.10337855709844 = beta_random(2, 5) * 10087 bin_idx→ 5 = min(9, int(value53.10337855709844 // 10))88 beta_dist[bin_idx]→ 3 += 1value ← 6.52870224702643, bin_idx ← 0, beta_dist[bin_idx] ← 4
85for _ in range(trials):86 value→ 6.52870224702643 = beta_random(2, 5) * 10087 bin_idx→ 0 = min(9, int(value6.52870224702643 // 10))88 beta_dist[bin_idx]→ 4 += 1value ← 21.119022916314467, bin_idx ← 2, beta_dist[bin_idx] ← 9
85for _ in range(trials):86 value→ 21.119022916314467 = beta_random(2, 5) * 10087 bin_idx→ 2 = min(9, int(value21.119022916314467 // 10))88 beta_dist[bin_idx]→ 9 += 1value ← 23.45052659715563, bin_idx ← 2, beta_dist[bin_idx] ← 10
85for _ in range(trials):86 value→ 23.45052659715563 = beta_random(2, 5) * 10087 bin_idx→ 2 = min(9, int(value23.45052659715563 // 10))88 beta_dist[bin_idx]→ 10 += 1value ← 26.41250227710408, bin_idx ← 2, beta_dist[bin_idx] ← 11
85for _ in range(trials):86 value→ 26.41250227710408 = beta_random(2, 5) * 10087 bin_idx→ 2 = min(9, int(value26.41250227710408 // 10))88 beta_dist[bin_idx]→ 11 += 1value ← 42.23863691710291, bin_idx ← 4, beta_dist[bin_idx] ← 7
85for _ in range(trials):86 value→ 42.23863691710291 = beta_random(2, 5) * 10087 bin_idx→ 4 = min(9, int(value42.23863691710291 // 10))88 beta_dist[bin_idx]→ 7 += 1value ← 14.642839014069597, bin_idx ← 1, beta_dist[bin_idx] ← 11
85for _ in range(trials):86 value→ 14.642839014069597 = beta_random(2, 5) * 10087 bin_idx→ 1 = min(9, int(value14.642839014069597 // 10))88 beta_dist[bin_idx]→ 11 += 1value ← 24.382716386688365, bin_idx ← 2, beta_dist[bin_idx] ← 12
85for _ in range(trials):86 value→ 24.382716386688365 = beta_random(2, 5) * 10087 bin_idx→ 2 = min(9, int(value24.382716386688365 // 10))88 beta_dist[bin_idx]→ 12 += 1value ← 11.830576162661888, bin_idx ← 1, beta_dist[bin_idx] ← 12
85for _ in range(trials):86 value→ 11.830576162661888 = beta_random(2, 5) * 10087 bin_idx→ 1 = min(9, int(value11.830576162661888 // 10))88 beta_dist[bin_idx]→ 12 += 1value ← 14.137386622455084, bin_idx ← 1, beta_dist[bin_idx] ← 13
85for _ in range(trials):86 value→ 14.137386622455084 = beta_random(2, 5) * 10087 bin_idx→ 1 = min(9, int(value14.137386622455084 // 10))88 beta_dist[bin_idx]→ 13 += 1value ← 13.221319845743562, bin_idx ← 1, beta_dist[bin_idx] ← 14
85for _ in range(trials):86 value→ 13.221319845743562 = beta_random(2, 5) * 10087 bin_idx→ 1 = min(9, int(value13.221319845743562 // 10))88 beta_dist[bin_idx]→ 14 += 1value ← 53.92859863305224, bin_idx ← 5, beta_dist[bin_idx] ← 4
85for _ in range(trials):86 value→ 53.92859863305224 = beta_random(2, 5) * 10087 bin_idx→ 5 = min(9, int(value53.92859863305224 // 10))88 beta_dist[bin_idx]→ 4 += 1value ← 25.780497632191334, bin_idx ← 2, beta_dist[bin_idx] ← 13
85for _ in range(trials):86 value→ 25.780497632191334 = beta_random(2, 5) * 10087 bin_idx→ 2 = min(9, int(value25.780497632191334 // 10))88 beta_dist[bin_idx]→ 13 += 1value ← 20.031038312422254, bin_idx ← 2, beta_dist[bin_idx] ← 14
85for _ in range(trials):86 value→ 20.031038312422254 = beta_random(2, 5) * 10087 bin_idx→ 2 = min(9, int(value20.031038312422254 // 10))88 beta_dist[bin_idx]→ 14 += 1value ← 49.26648632722122, bin_idx ← 4, beta_dist[bin_idx] ← 8
85for _ in range(trials):86 value→ 49.26648632722122 = beta_random(2, 5) * 10087 bin_idx→ 4 = min(9, int(value49.26648632722122 // 10))88 beta_dist[bin_idx]→ 8 += 1bar ← ██
pass 1 of 1090for i0, count4 in enumerate(beta_dist[4, 14, 14, 5, 8, 4, 0, 1, 0, 0]):91 bar→ ██ = '█' * max(1, count4 // 2)92 print(f"[{i0*10}-{(i+1)*10}): {count4:4d} ({count/trials50*100:.1f}%) {bar██}")All 10 passes — pass 1 is the card above pass icountbar1 0 4 ██ 2 1 14 ███████ 3 2 14 ███████ 4 3 5 ██ 5 4 8 ████ 6 5 4 ██ 7 6 0 █ 8 7 1 █ 9 8 0 █ 10 9 0 █ poisson ← [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
94# Poisson distribution95print("\nPoisson distribution (lambda=5):")96def poisson_random(lambda_param):97 """Generate Poisson random variable."""98 L = math.exp(-lambda_param)99 k = 0100 p = 1.0101 while p > L:102 k += 1103 p *= random.random()104 return k - 1105106poisson→ [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] = [0] * 15107for _ in range(trials):output Poisson distribution (lambda=5):for _ in range(trials):
pass 1 of 50106poisson = [0] * 15107for _0 in range(trials50):108 events = poisson_random(5.0)109 if events < len(poisson):50 passes — pass 1 is the card above pass _1 0 2 1 3 2 4 3 5 4 6 5 7 6 8 7 9 8 ⋯ 39 more passes ⋯ 49 48 50 49 L ← 0.006737946999085467, k ← 0, p ← 1.0
pass 1 of 5095print("\nPoisson distribution (lambda=5):")96def poisson_random(lambda_param5.0):97 """Generate Poisson random variable."""98 L→ 0.006737946999085467 = math<module 'math' from '/usr/local/lib/python3.12/lib-dynload/math.cpython-312-x86_64-linux-gnu.so'>.exp(-lambda_param5.0)99 k→ 0 = 0100 p→ 1.0 = 1.0101 while p > L:50 passes — pass 1 is the card above pass Lkp1 0.006737946999085467 0 1.0 2 0.006737946999085467 0 1.0 3 0.006737946999085467 0 1.0 4 0.006737946999085467 0 1.0 5 0.006737946999085467 0 1.0 6 0.006737946999085467 0 1.0 7 0.006737946999085467 0 1.0 8 0.006737946999085467 0 1.0 9 0.006737946999085467 0 1.0 ⋯ 39 more passes ⋯ 49 0.006737946999085467 0 1.0 50 0.006737946999085467 0 1.0 k ← 1, p ← 0.8570893372705127
pass 1 of 299100p = 1.0101while p1.0 > L0.006737946999085467:102 k→ 1 += 1103 p→ 0.8570893372705127 *= random<module 'random' from '/usr/local/lib/python3.12/random.py'>.random()104return k - 1299 passes — pass 1 is the card above pass kp1 0 → 1 1.0 → 0.8570893372705127 2 1 → 2 0.8570893372705127 → 0.8210856630186273 3 2 → 3 0.8210856630186273 → 0.6962893422631077 4 3 → 4 0.6962893422631077 → 0.25846019147263827 5 4 → 5 0.25846019147263827 → 0.08152271391120888 6 5 → 6 0.08152271391120888 → 0.07321727767721799 7 6 → 7 0.07321727767721799 → 0.0645034880069813 8 7 → 8 0.0645034880069813 → 0.037058503934340424 9 8 → 9 0.037058503934340424 → 0.010181195182942224 ⋯ 288 more passes ⋯ 298 2 → 3 0.2736554994286034 → 0.06860440506325886 299 3 → 4 0.06860440506325886 → 0.002709087979810306 return k - 1
103 p *= random.random()104return k10 - 1events ← 9
107for _ in range(trials):108 events→ 9 = poisson_random(5.0)109 if events < len(poisson):poisson[events] ← 1
pass 1 of 50108events = poisson_random(5.0)109if events9 < len(poisson[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]):110 poisson[events]→ 1 += 150 passes — pass 1 is the card above pass eventspoissonpoisson[events]1 9 [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] 0 → 1 2 6 [0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0] 0 → 1 3 5 [0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0] 0 → 1 4 3 [0, 0, 0, 0, 0, 1, 1, 0, 0, 1, 0, 0, 0, 0, 0] 0 → 1 5 5 [0, 0, 0, 1, 0, 1, 1, 0, 0, 1, 0, 0, 0, 0, 0] 1 → 2 6 4 [0, 0, 0, 1, 0, 2, 1, 0, 0, 1, 0, 0, 0, 0, 0] 0 → 1 7 6 [0, 0, 0, 1, 1, 2, 1, 0, 0, 1, 0, 0, 0, 0, 0] 1 → 2 8 11 [0, 0, 0, 1, 1, 2, 2, 0, 0, 1, 0, 0, 0, 0, 0] 0 → 1 9 5 [0, 0, 0, 1, 1, 2, 2, 0, 0, 1, 0, 1, 0, 0, 0] 2 → 3 ⋯ 39 more passes ⋯ 49 7 [0, 2, 4, 8, 5, 12, 8, 2, 2, 4, 0, 1, 0, 0, 0] 2 → 3 50 3 [0, 2, 4, 8, 5, 12, 8, 3, 2, 4, 0, 1, 0, 0, 0] 8 → 9 return k - 1
103 p *= random.random()104return k7 - 1events ← 6
107for _ in range(trials):108 events→ 6 = poisson_random(5.0)109 if events < len(poisson):return k - 1
103 p *= random.random()104return k6 - 1events ← 5
107for _ in range(trials):108 events→ 5 = poisson_random(5.0)109 if events < len(poisson):return k - 1
103 p *= random.random()104return k4 - 1events ← 3
107for _ in range(trials):108 events→ 3 = poisson_random(5.0)109 if events < len(poisson):return k - 1
103 p *= random.random()104return k6 - 1events ← 5
107for _ in range(trials):108 events→ 5 = poisson_random(5.0)109 if events < len(poisson):return k - 1
103 p *= random.random()104return k5 - 1events ← 4
107for _ in range(trials):108 events→ 4 = poisson_random(5.0)109 if events < len(poisson):return k - 1
103 p *= random.random()104return k7 - 1events ← 6
107for _ in range(trials):108 events→ 6 = poisson_random(5.0)109 if events < len(poisson):return k - 1
103 p *= random.random()104return k12 - 1events ← 11
107for _ in range(trials):108 events→ 11 = poisson_random(5.0)109 if events < len(poisson):return k - 1
103 p *= random.random()104return k6 - 1events ← 5
107for _ in range(trials):108 events→ 5 = poisson_random(5.0)109 if events < len(poisson):return k - 1
103 p *= random.random()104return k4 - 1events ← 3
107for _ in range(trials):108 events→ 3 = poisson_random(5.0)109 if events < len(poisson):return k - 1
103 p *= random.random()104return k3 - 1events ← 2
107for _ in range(trials):108 events→ 2 = poisson_random(5.0)109 if events < len(poisson):return k - 1
103 p *= random.random()104return k5 - 1events ← 4
107for _ in range(trials):108 events→ 4 = poisson_random(5.0)109 if events < len(poisson):return k - 1
103 p *= random.random()104return k7 - 1events ← 6
107for _ in range(trials):108 events→ 6 = poisson_random(5.0)109 if events < len(poisson):return k - 1
103 p *= random.random()104return k8 - 1events ← 7
107for _ in range(trials):108 events→ 7 = poisson_random(5.0)109 if events < len(poisson):return k - 1
103 p *= random.random()104return k7 - 1events ← 6
107for _ in range(trials):108 events→ 6 = poisson_random(5.0)109 if events < len(poisson):return k - 1
103 p *= random.random()104return k7 - 1events ← 6
107for _ in range(trials):108 events→ 6 = poisson_random(5.0)109 if events < len(poisson):return k - 1
103 p *= random.random()104return k10 - 1events ← 9
107for _ in range(trials):108 events→ 9 = poisson_random(5.0)109 if events < len(poisson):return k - 1
103 p *= random.random()104return k5 - 1events ← 4
107for _ in range(trials):108 events→ 4 = poisson_random(5.0)109 if events < len(poisson):return k - 1
103 p *= random.random()104return k6 - 1events ← 5
107for _ in range(trials):108 events→ 5 = poisson_random(5.0)109 if events < len(poisson):return k - 1
103 p *= random.random()104return k4 - 1events ← 3
107for _ in range(trials):108 events→ 3 = poisson_random(5.0)109 if events < len(poisson):return k - 1
103 p *= random.random()104return k6 - 1events ← 5
107for _ in range(trials):108 events→ 5 = poisson_random(5.0)109 if events < len(poisson):return k - 1
103 p *= random.random()104return k6 - 1events ← 5
107for _ in range(trials):108 events→ 5 = poisson_random(5.0)109 if events < len(poisson):return k - 1
103 p *= random.random()104return k3 - 1events ← 2
107for _ in range(trials):108 events→ 2 = poisson_random(5.0)109 if events < len(poisson):return k - 1
103 p *= random.random()104return k10 - 1events ← 9
107for _ in range(trials):108 events→ 9 = poisson_random(5.0)109 if events < len(poisson):return k - 1
103 p *= random.random()104return k10 - 1events ← 9
107for _ in range(trials):108 events→ 9 = poisson_random(5.0)109 if events < len(poisson):return k - 1
103 p *= random.random()104return k4 - 1events ← 3
107for _ in range(trials):108 events→ 3 = poisson_random(5.0)109 if events < len(poisson):return k - 1
103 p *= random.random()104return k2 - 1events ← 1
107for _ in range(trials):108 events→ 1 = poisson_random(5.0)109 if events < len(poisson):return k - 1
103 p *= random.random()104return k6 - 1events ← 5
107for _ in range(trials):108 events→ 5 = poisson_random(5.0)109 if events < len(poisson):return k - 1
103 p *= random.random()104return k9 - 1events ← 8
107for _ in range(trials):108 events→ 8 = poisson_random(5.0)109 if events < len(poisson):return k - 1
103 p *= random.random()104return k7 - 1events ← 6
107for _ in range(trials):108 events→ 6 = poisson_random(5.0)109 if events < len(poisson):return k - 1
103 p *= random.random()104return k9 - 1events ← 8
107for _ in range(trials):108 events→ 8 = poisson_random(5.0)109 if events < len(poisson):return k - 1
103 p *= random.random()104return k3 - 1events ← 2
107for _ in range(trials):108 events→ 2 = poisson_random(5.0)109 if events < len(poisson):return k - 1
103 p *= random.random()104return k6 - 1events ← 5
107for _ in range(trials):108 events→ 5 = poisson_random(5.0)109 if events < len(poisson):return k - 1
103 p *= random.random()104return k3 - 1events ← 2
107for _ in range(trials):108 events→ 2 = poisson_random(5.0)109 if events < len(poisson):return k - 1
103 p *= random.random()104return k5 - 1events ← 4
107for _ in range(trials):108 events→ 4 = poisson_random(5.0)109 if events < len(poisson):return k - 1
103 p *= random.random()104return k2 - 1events ← 1
107for _ in range(trials):108 events→ 1 = poisson_random(5.0)109 if events < len(poisson):return k - 1
103 p *= random.random()104return k7 - 1events ← 6
107for _ in range(trials):108 events→ 6 = poisson_random(5.0)109 if events < len(poisson):return k - 1
103 p *= random.random()104return k4 - 1events ← 3
107for _ in range(trials):108 events→ 3 = poisson_random(5.0)109 if events < len(poisson):return k - 1
103 p *= random.random()104return k6 - 1events ← 5
107for _ in range(trials):108 events→ 5 = poisson_random(5.0)109 if events < len(poisson):return k - 1
103 p *= random.random()104return k4 - 1events ← 3
107for _ in range(trials):108 events→ 3 = poisson_random(5.0)109 if events < len(poisson):return k - 1
103 p *= random.random()104return k6 - 1events ← 5
107for _ in range(trials):108 events→ 5 = poisson_random(5.0)109 if events < len(poisson):return k - 1
103 p *= random.random()104return k7 - 1events ← 6
107for _ in range(trials):108 events→ 6 = poisson_random(5.0)109 if events < len(poisson):return k - 1
103 p *= random.random()104return k6 - 1events ← 5
107for _ in range(trials):108 events→ 5 = poisson_random(5.0)109 if events < len(poisson):return k - 1
103 p *= random.random()104return k5 - 1events ← 4
107for _ in range(trials):108 events→ 4 = poisson_random(5.0)109 if events < len(poisson):return k - 1
103 p *= random.random()104return k4 - 1events ← 3
107for _ in range(trials):108 events→ 3 = poisson_random(5.0)109 if events < len(poisson):return k - 1
103 p *= random.random()104return k8 - 1events ← 7
107for _ in range(trials):108 events→ 7 = poisson_random(5.0)109 if events < len(poisson):return k - 1
103 p *= random.random()104return k6 - 1events ← 5
107for _ in range(trials):108 events→ 5 = poisson_random(5.0)109 if events < len(poisson):return k - 1
103 p *= random.random()104return k4 - 1events ← 3
107for _ in range(trials):108 events→ 3 = poisson_random(5.0)109 if events < len(poisson):return k - 1
103 p *= random.random()104return k8 - 1events ← 7
107for _ in range(trials):108 events→ 7 = poisson_random(5.0)109 if events < len(poisson):return k - 1
103 p *= random.random()104return k4 - 1events ← 3
107for _ in range(trials):108 events→ 3 = poisson_random(5.0)109 if events < len(poisson):print("Number of events:")
112print("Number of events:")113for i, count in enumerate(poisson[:12]):outputNumber of events:for i, count in enumerate(poisson[:12]):
pass 1 of 12112print("Number of events:")113for i0, count0 in enumerate(poisson[:12][0, 2, 4, 9, 5, 12, 8, 3, 2, 4, 0, 1]):114 print(f"{i0:2d}: {count0:4d} ({count/trials50*100:.1f}%)")output 0: 0 (0.0%)All 12 passes — pass 1 is the card above pass icount1 0 0 2 1 2 3 2 4 4 3 9 5 4 5 6 5 12 7 6 8 8 7 3 9 8 2 10 9 4 11 10 0 12 11 1
Exercise: practical.py
Create a dice game that rolls multiple dice and determines the winner