random.SystemRandom

Full name
random.SystemRandom
Library
random
Syntax

random.SystemRandom([seed])

Description

random.SystemRandom is a class that uses the os.urandom() function to generate random numbers from sources provided by the operating system. This class does not rely on software state and the generated number sequences are not reproducible. This means that the seed() method has no effect and is ignored.

We can see the set of names associated with this class with the following code:

names = lambda obj: print([n for n in dir(obj) if not n.startswith("_")])
names(random.SystemRandom())
['VERSION', 'betavariate', 'choice', 'choices', 'expovariate', 'gammavariate', 'gauss', 'gauss_next', 'getrandbits', 'getstate', 'lognormvariate', 'normalvariate', 'paretovariate', 'randint', 'random', 'randrange', 'sample', 'seed', 'setstate', 'shuffle', 'triangular', 'uniform', 'vonmisesvariate', 'weibullvariate']

We see that it includes many of the functions available in the random library.

Examples

We can obtain a random number in the range [0, 1) extracted from a uniform distribution with the following code:

random.SystemRandom().random()
0.012833647854822439

Similarly, we can obtain a random integer number in the range [11, 16] extracted from a uniform distribution with the following code:

random.SystemRandom().randint(11, 16)
13
Submitted by admin on Sun, 03/21/2021 - 11:09