1def randint(a, b):
2 "Return random integer in range [a, b], including both end points."
3 return a + randbelow(b - a + 1)
4
5def randbelow(n):
6 "Return a random int in the range [0,n). Raises ValueError if n<=0."
7 k = n.bit_length()
8 numbytes = (k + 7) // 8
9 while True:
10 r = int.from_bytes(random_bytes(numbytes), 'big')
11 r >>= numbytes * 8 - k
12 if r < n:
13 return r
14
15def random_bytes(n):
16 "Return n random bytes"
17 with open('/dev/urandom', 'rb') as file:
18 return file.read(n)
19
20print(randint(1, 10000))