Random-Module
Sun 29 June 2025
import random
print(random.random())
0.487560015622336
print(random.randint(1, 10))
6
print(random.randrange(0, 20, 2))
12
print(random.choice(['red', 'green', 'blue']))
red
print(random.choices(['A', 'B', 'C'], k=5))
['C', 'B', 'C', 'B', 'A']
print(random.choices(['A', 'B', 'C'], k=5))
['A', 'C', 'C', 'C', 'B']
print(random.uniform(10, 20))
17.44199624104448
print(random.getrandbits(4))
10
print(random.choice((10, 20, 30)))
20
print(random.choice("Python"))
y
s = list("hello")
random.shuffle(s)
print("".join(s))
helol
colors = ['red', 'blue', 'green']
print(colors[random.randrange(len(colors))])
red
print(random.sample(range(1, 20), 5))
[3, 16, 6, 11, 13]
print(random.randint(1, 6))
5
print("Heads" if random.randint(0, 1) == 0 else "Tails")
Heads
print(round(random.uniform(0, 100), 2))
23.06
print(random.choice([True, False]))
True
print(random.choices(range(10), k=3))
[2, 4, 6]
print(random.choices(['low', 'mid', 'high'], weights=[1, 3, 6], k=1))
['mid']
print(random.choices(['x', 'y', 'z'], k=3))
['y', 'x', 'z']
#Seeding
random.seed(10)
print(random.random())
0.5714025946899135
random.seed(123)
print(random.randint(1, 100))
7
random.seed(50)
a = random.random()
random.seed(50)
b = random.random()
print(a == b) # True
True
import time
random.seed(time.time())
random.seed("hello")
print(random.random())
0.3537754404730722
def get_random_with_seed(seed_val):
random.seed(seed_val)
return random.randint(1, 100)
print(get_random_with_seed(42))
82
lst = [1, 2, 3, 4, 5]
random.seed(2)
random.shuffle(lst)
print(lst)
[3, 2, 4, 5, 1]
random.seed(4)
print(random.sample(range(1, 100), 5))
[31, 39, 14, 93, 51]
random.seed(999)
print(random.random())
0.7813468849570298
for i in range(5):
random.seed(i)
print(random.randint(1, 10))
7
3
1
4
4
#Statistical Distributions
print(random.uniform(1, 10))
3.7296872729695907
print(random.gauss(0, 1))
-0.20535732231241616
print(random.normalvariate(0, 1))
0.11926168260455533
print(random.triangular(1, 100, 50))
92.97311499109534
print(random.betavariate(2, 5))
0.08904379481080787
print(random.expovariate(1.5))
1.1691481464621643
print(random.gammavariate(2, 3))
3.297017627578116
print(random.lognormvariate(0, 0.5))
0.753985601257307
print(random.vonmisesvariate(0, 1))
0.25594792444230136
print(random.paretovariate(2.5))
3.6578959596970564
print(random.weibullvariate(1, 1.5))
1.5272270713856133
scores = [random.normalvariate(70, 10) for _ in range(5)]
print(scores)
[55.89574778343234, 65.44265505925276, 64.27624374716605, 57.90449889121083, 56.24625556120736]
from collections import Counter
values = [round(random.gauss(50, 15)) for _ in range(1000)]
hist = Counter(values)
print(hist.most_common(5))
[(58, 36), (46, 32), (43, 30), (47, 28), (53, 28)]
values = [random.gauss(50, 5) for _ in range(1000)]
print(min(values), max(values))
29.66666037117583 67.03237989352832
import statistics
data = [random.gauss(100, 20) for _ in range(1000)]
print(statistics.mean(data), statistics.stdev(data))
100.99849912914817 21.406751963222828
print(random.triangular(0, 10, 8))
4.979361550949488
print(random.normalvariate(170, 10))
162.17402561610336
print(random.lognormvariate(0.02, 0.05))
0.9653176706571646
print(random.weibullvariate(1, 1.5))
1.043084668057772
print([round(random.gammavariate(2, 1), 2) for _ in range(5)])
[0.93, 2.61, 2.97, 1.65, 3.69]
print(random.choice(["rock", "paper", "scissors"]))
paper
for _ in range(10):
print("Heads" if random.randint(0, 1) == 0 else "Tails")
Tails
Tails
Heads
Heads
Heads
Tails
Heads
Tails
Heads
Heads
print([random.randint(1, 6) for _ in range(5)])
[5, 5, 3, 4, 3]
print(random.randint(0, 36))
30
suits = ['Hearts', 'Diamonds', 'Clubs', 'Spades']
ranks = ['2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K', 'A']
deck = [f"{r} of {s}" for r in ranks for s in suits]
print(random.choice(deck))
7 of Hearts
random.shuffle(deck)
print(deck[:5])
['3 of Hearts', '6 of Hearts', '8 of Spades', '5 of Clubs', '5 of Diamonds']
print(sum([random.randint(1, 6) for _ in range(2)]))
8
d1, d2 = random.randint(1, 6), random.randint(1, 6)
print("Snake eyes!" if d1 == d2 == 1 else f"{d1}, {d2}")
4, 5
print(random.sample(range(1, 50), 6))
[6, 12, 2, 35, 28, 18]
makes = sum(random.choices([1, 0], weights=[0.8, 0.2], k=10))
print(f"Makes: {makes}")
Makes: 6
print([random.choice(['H', 'T']) for _ in range(100)].count('H'))
46
print(random.uniform(0, 360))
327.2638114387294
print((random.randint(0, 255), random.randint(0, 255), random.randint(0, 255)))
(166, 13, 129)
def play_rps():
return random.choice(['rock', 'paper', 'scissors'])
print(play_rps())
paper
print(random.randint(1990, 2025))
1991
print(random.random() < 0.5)
True
print(random.choice(['up', 'down', 'left', 'right']))
right
print(random.randint(1, 8))
3
print(sorted([random.randint(1, 6) for _ in range(3)]))
[1, 3, 4]
print(random.choices([1,2,3,4,5,6], weights=[1,1,1,1,1,5], k=1))
[6]
import string
# 71. Random digit
print(random.choice(string.digits))
6
print(random.choice(string.ascii_lowercase))
g
print(random.choice(string.ascii_uppercase))
I
print(random.choice(string.ascii_uppercase))
K
print(''.join(random.choices(string.digits, k=6)))
946030
chars = string.ascii_letters + string.digits
print(''.join(random.choices(chars, k=8)))
P4PPbPxx
chars = string.ascii_letters + string.digits + string.punctuation
print(''.join(random.choices(chars, k=10)))
%u?q"6SMhU
passwords = [''.join(random.choices(chars, k=8)) for _ in range(5)]
print(passwords)
['KqbL3F>0', "'8M(CuE!", 'lm)Sy>@m', 'Q,.jFUd[', 'yGAE"-Xv']
pwd = random.choice(string.ascii_uppercase) + \
random.choice(string.digits) + \
random.choice(string.punctuation) + \
''.join(random.choices(string.ascii_lowercase, k=5))
print(''.join(random.sample(pwd, len(pwd))))
`3afWfhy
print(''.join(random.choices(string.ascii_letters + string.digits, k=32)))
pFAS20nvJS9P5H9L9Wzkk8jkIpgGP89y
mac = ":".join(f"{random.randint(0, 255):02x}" for _ in range(6))
print(mac)
d4:07:fa:5f:8e:cf
print('#' + ''.join(random.choices('0123456789ABCDEF', k=6)))
#4F5359
print(''.join(random.choices(string.ascii_uppercase, k=2)) + '-' + ''.join(random.choices(string.digits, k=4)))
JA-1239
print('user' + str(random.randint(1000, 9999)))
user4541
print('file_' + str(random.randint(10000, 99999)) + '.txt')
file_96398.txt
print(random.choice(['.txt', '.csv', '.json', '.xml']))
.json
print(''.join(random.choices('0123456789abcdef', k=4)))
7188
print('user_' + ''.join(random.choices(string.ascii_lowercase + string.digits, k=6)))
user_4d1wmt
print(''.join(random.choices(string.digits, k=4)))
7898
print(''.join(random.choices(string.ascii_letters + string.punctuation + string.digits, k=12)))
YyyP<ze'>r>A
#Custom Random Logic & Utilities
print('Heads' if random.random() < 0.7 else 'Tails')
Heads
print(random.choice(['Mon', 'Tue', 'Wed', 'Thu', 'Fri']))
Fri
def random_bool(p=0.5): return random.random() < p
print(random_bool())
True
def random_hex_color(): return '#{:02X}{:02X}{:02X}'.format(*[random.randint(0, 255) for _ in range(3)])
print(random_hex_color())
#669C11
print((random.uniform(-10, 10), random.uniform(-10, 10)))
(6.730485397285662, 1.5518531002953715)
import math
theta = random.uniform(0, 2 * math.pi)
print((math.cos(theta), math.sin(theta)))
(-0.6462767356050989, 0.7631031260685656)
s = "abcdef"
print(''.join(random.sample(s, len(s))))
beadfc
print(random.choices(['Yes', 'No'], weights=[0.2, 0.8])[0])
No
print(random.choice(list(d.keys())))
a
print(random.choice(list(d.values())))
2
print(random.choice(list({1, 2, 3, 4, 5})))
1
print(random.sample(['a', 'b', 'c', 'd', 'e'], 3))
['b', 'c', 'a']
matrix = [[random.randint(0, 9) for _ in range(3)] for _ in range(3)]
print(matrix)
[[1, 5, 2], [6, 2, 1], [5, 7, 5]]
print([[random.random() for _ in range(2)] for _ in range(2)])
[[0.32336223690360333, 0.7452373502161956], [0.14864018081208896, 0.7484333575783582]]
print([random.uniform(0, 1) for _ in range(5)])
[0.5246443223938702, 0.4798894073886164, 0.6134321478562901, 0.6963209505143704, 0.8455446857221685]
print(random.sample(range(5), 5))
[0, 2, 4, 1, 3]
print(random.choice(list(d.items())))
('a', 1)
import time; delay = random.uniform(1, 3); print(f"Sleeping {delay:.2f}s"); time.sleep(delay)
Sleeping 2.35s
position = 0
for _ in range(10):
position += random.choice([-1, 1])
print(position)
2
x = y = 0
for _ in range(10):
dx, dy = random.choice([(1,0), (-1,0), (0,1), (0,-1)])
x += dx; y += dy
print(x, y)
0 0
inside = 0
for _ in range(10000):
x, y = random.random(), random.random()
if x**2 + y**2 < 1: inside += 1
print(4 * inside / 10000)
3.1192
big_list = list(range(1000))
random.shuffle(big_list)
print(big_list[:10])
[107, 724, 665, 158, 136, 116, 612, 664, 569, 330]
print([random.choice(['Yes', 'No']) for _ in range(20)])
['Yes', 'No', 'Yes', 'Yes', 'Yes', 'Yes', 'Yes', 'Yes', 'Yes', 'No', 'Yes', 'No', 'Yes', 'No', 'No', 'Yes', 'Yes', 'No', 'Yes', 'Yes']
print(random.sample("abcdefg", 3))
['e', 'c', 'a']
nested = {'a': {'x': 1}, 'b': {'y': 2}}
print(random.choice(list(nested.values())))
{'y': 2}
print(f"{random.randint(0,23):02}:{random.randint(0,59):02}")
08:25
print(f"log_{random.randint(100,999)}_{int(time.time())}.txt")
log_851_1751208941.txt
lines = ["first", "second", "third"]
print(random.choice(lines))
first
def get_random_index(data): return random.randrange(len(data))
print(get_random_index(["apple", "banana", "cherry"]))
2
print([random.choice([True, False]) for _ in range(10)])
[True, False, False, True, False, True, False, False, True, False]
print(random.random() < 0.3)
False
print(random.choices(string.ascii_lowercase, k=5))
['x', 'q', 'h', 'l', 'c']
print(random.choices(string.digits, k=4))
['1', '4', '6', '4']
print(time.time() + random.randint(1000, 10000))
1751216346.3365538
target = 50
print(target + random.uniform(-5, 5))
50.356717471190464
print(f'{random.uniform(0, 100):.2f}%')
69.27%
delay = random.uniform(1, 5)
print(f'Delay: {delay:.1f}s')
Delay: 2.5s
print(random.choice(['Keep going!', 'Try again.', 'Well done!']))
Well done!
print(random.choice([x for x in range(3, 100, 3)]))
84
#General Random Values
for _ in range(30):
print(random.randint(1, 100), random.uniform(0, 1))
84 0.3767823050705269
34 0.4996558877437687
79 0.691240369351869
99 0.26840410858727115
7 0.7098460181243668
5 0.8754797296152051
9 0.09412814389266544
78 0.9096059220019942
5 0.8753188411501845
77 0.5846329189786156
28 0.30413800514907685
43 0.33514783037780593
97 0.009804734092744627
85 0.39609040485009817
43 0.34107999905789
29 0.16559751163388514
78 0.43778708133113475
12 0.7431437237246094
84 0.9447745286454454
95 0.15925379390082173
93 0.4953117694259682
95 0.43577383206430387
72 0.9096394203849415
26 0.22632158475619757
71 0.8691104439723106
97 0.3810684423581878
68 0.2778499542183084
84 0.49898121680846474
95 0.3955579548284107
72 0.442355738396508
#Random Sampling & Generation
print(random.choice(range(0, 100, 2)))
52
print(random.choice(range(1, 100, 2)))
45
primes = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29]
print(random.choice(primes))
29
print(random.choice('aeiou'))
o
print(random.choice([c for c in string.ascii_lowercase if c not in 'aeiou']))
m
words = ['life', 'is', 'beautiful', 'random', 'cool', 'great']
print(' '.join(random.choices(words, k=3)))
cool beautiful life
print(random.choice(string.printable))
l
print(random.choice(range(0, 101, 10)))
0
print(random.randint(100000, 999999))
490960
print(''.join(random.choices('0123456789abcdef', k=8)))
c8a45e7c
print(''.join(random.choices(string.ascii_uppercase, k=2)))
ZJ
print('-'.join(random.choices(words, k=3)))
great-cool-beautiful
print(f"${random.uniform(1, 500):.2f}")
$181.62
print(tuple(random.randint(1, 10) for _ in range(3)))
(1, 3, 4)
print(random.choices(['Pass', 'Fail'], weights=[90, 10], k=1)[0])
Pass
print('ID-' + ''.join(random.choices(string.ascii_uppercase, k=5)))
ID-MVUSH
print(f"{random.randint(1,12)}:{random.randint(0,59):02} {'AM' if random.random()<0.5 else 'PM'}")
11:08 PM
print(''.join(random.choices('01', k=8)))
01010000
print(''.join(random.choices('012', k=6)))
022102
print([[random.randint(0,9) for _ in range(2)] for _ in range(2)])
[[7, 0], [0, 7]]
Score: 155
Category: basics