June 2018
Beginner
508 pages
13h 8m
English
We can use three functions in order to deal with random numbers:
# secrs/secr_rand.pyimport secretsprint(secrets.choice('Choose one of these words'.split()))print(secrets.randbelow(10 ** 6))print(secrets.randbits(32))
The first one, choice, picks an element at random from a non-empty sequence. The second one, randbelow, generates a random integer between 0 and the argument you call it with, and the third one, randbits, generates an integer with n random bits in it. Running that code produces the following output (which is always different):
$ python secr_rand.pyone5041563172492450
You should use these functions instead of those from the random module whenever you need randomness in the context of cryptography, as these are ...
Read now
Unlock full access