MCPcopy Index your code
hub / github.com/python/cpython / Random

Class Random

Lib/random.py:103–882  ·  view source on GitHub ↗

Random number generator base class used by bound module functions. Used to instantiate instances of Random to get generators that don't share state. Class Random can also be subclassed if you want to use a different basic generator of your own devising: in that case, override the f

Source from the content-addressed store, hash-verified

101
102
103class Random(_random.Random):
104 """Random number generator base class used by bound module functions.
105
106 Used to instantiate instances of Random to get generators that don't
107 share state.
108
109 Class Random can also be subclassed if you want to use a different basic
110 generator of your own devising: in that case, override the following
111 methods: random(), seed(), getstate(), and setstate().
112 Optionally, implement a getrandbits() method so that randrange()
113 can cover arbitrarily large ranges.
114
115 """
116
117 VERSION = 3 # used by getstate/setstate
118
119 def __init__(self, x=None):
120 """Initialize an instance.
121
122 Optional argument x controls seeding, as for Random.seed().
123 """
124
125 self.seed(x)
126 self.gauss_next = None
127
128 def seed(self, a=None, version=2):
129 """Initialize internal state from a seed.
130
131 The only supported seed types are None, int, float,
132 str, bytes, and bytearray.
133
134 None or no argument seeds from current time or from an operating
135 system specific randomness source if available.
136
137 If *a* is an int, all bits are used.
138
139 For version 2 (the default), all of the bits are used if *a* is a str,
140 bytes, or bytearray. For version 1 (provided for reproducing random
141 sequences from older versions of Python), the algorithm for str and
142 bytes generates a narrower range of seeds.
143
144 """
145
146 if version == 1 and isinstance(a, (str, bytes)):
147 a = a.decode('latin-1') if isinstance(a, bytes) else a
148 x = ord(a[0]) << 7 if a else 0
149 for c in map(ord, a):
150 x = ((1000003 * x) ^ c) & 0xFFFFFFFFFFFFFFFF
151 x ^= len(a)
152 a = -2 if x == -1 else x
153
154 elif version == 2 and isinstance(a, (str, bytes, bytearray)):
155 global _sha512
156 if _sha512 is None:
157 try:
158 # hashlib is pretty heavy to load, try lean internal
159 # module first
160 from _sha2 import sha512 as _sha512

Callers 3

test_ignore_zerosMethod · 0.90
random.pyFile · 0.85

Calls

no outgoing calls

Tested by 2

test_ignore_zerosMethod · 0.72

Used in the wild real call sites across dependent graphs

searching dependent graphs…