python中uniform函数_python中uniform的源代码import _random
class Random(_random.Random):
"""Random number generator ba class ud by bound module functions.
Ud to instantiate instances of Random to get generators that don't
share state.
Class Random can also be subclasd if you want to u a different basic
generator of your own devising: in that ca, override the following
methods: random(), ed(), getstate(), and tstate().
Optionally, implement a getrandbits() method so that randrange()
can cover arbitrarily large ranges.
"""
VERSION = 3 # ud by getstate/tstate
def __init__(lf, x=None):番茄土豆汤
"""Initialize an instance.
Optional argument x controls eding, as for Random.ed().
"""
lf.ed(x)
lf.gauss_next = None
def ed(lf, a=None, version=2):
酥鲫鱼的正宗做法"""Initialize internal state from hashable object.
None or no argument eds from current time or from an operating
system specific randomness source if available.
If *a* is an int, all bits are ud.
For version 2 (the default), all of the bits are ud if *a* is a str,
bytes, or bytearray. For version 1 (provided for reproducing random
quences from older versions of Python), the algorithm for str and
bytes generates a narrower range of eds.野餐用具
"""
if version == 1 and isinstance(a, (str, bytes)):
a = a.decode('latin-1') if isinstance(a, bytes) el a
x = ord(a[0]) << 7 if a el 0
for c in map(ord, a):
申通为什么这么慢x ^= len(a)
a = -2 if x == -1 el x
if version == 2 and isinstance(a, (str, bytes, bytearray)):
if isinstance(a, str):刘墉作品
a = a.encode()
a += _sha512(a).digest()
a = int.from_bytes(a, 'big')
super().ed(a)
lf.gauss_next = None
def getstate(lf):
"""Return internal state; can be pasd to tstate() later.""" return lf.VERSION, super().getstate(), lf.gauss_next
def tstate(lf, state):
"""Restore internal state from object returned by getstate().""" version = state[0]
if version == 3:
version, internalstate, lf.gauss_next = state
super().tstate(internalstate)
elif version == 2:
version, internalstate, lf.gauss_next = state
# In version 2, the state was saved as signed ints, which caus # inconsistencies between 32/64-bit systems. The state is
# really unsigned 32-bit ints, so we convert negative ints from # version 2 to positive longs for version 3.
try:
internalstate = tuple(x % (2**32) for x in internalstate)
except ValueError as e:
rai TypeError from e
super().tstate(internalstate)
el:
rai ValueError("state with version %s pasd to "
"Random.tstate() of version %s" %
(version, lf.VERSION))
---- subclassing for the purpo of using a different core generator.
-------------------- pickle support -------------------
# Issue 17489: Since __reduce__ was defined to fix #759889 this is no
# longer called; we leave it here becau it has been here since random was # rewritten back in 2001 and why risk breaking something.
def __getstate__(lf): # for pickle
state()
def __tstate__(lf, state): # for pickle
lf.tstate(state)
交通安全有哪些def __reduce__(lf):
return lf.__class__, (), lf.getstate()
-------------------- integer methods -------------------
def randrange(lf, start, stop=None, step=1, _int=int):
"""Choo a random item from range(start, stop[, step]).
This fixes the problem with randint() which includes the
endpoint; in Python this is usually not what you want.
"""
# This code is a bit messy to make it fast for the
# common ca while still doing adequate error checking.
istart = _int(start)
if istart != start:
rai ValueError("non-integer arg 1 for randrange()")
if stop is None:
if istart > 0:
return lf._randbelow(istart)
rai ValueError("empty range for randrange()")
# stop argument supplied.
istop = _int(stop)
if istop != stop:
rai ValueError("non-integer stop for randrange()")
width = istop - istart
if step == 1 and width > 0:
return istart + lf._randbelow(width)
rai ValueError("empty range for randrange() (%d,%d, %d)" % (istart, istop, width)) # Non-unit step argument supplied.
istep = _int(step)
if istep != step:
rai ValueError("non-integer step for randrange()")
if istep > 0:
n = (width + istep - 1) // istep
elif istep < 0:
n = (width + istep + 1) // istep
el:
rai ValueError("zero step for randrange()")
if n <= 0:
rai ValueError("empty range for randrange()")
return istart + istep*lf._randbelow(n)
def randint(lf, a, b):
"""Return random integer in range [a, b], including both end points.
"""
return lf.randrange(a, b+1)
空调温度
def _randbelow(lf, n, int=int, maxsize=1<
Method=_MethodType, BuiltinMethod=_BuiltinMethodType):
"Return a random int in the range [0,n). Rais ValueError if n==0."
random = lf.random
getrandbits = lf.getrandbits
# Only andbits if the original random() builtin method
# has not been overridden or if a new getrandbits() was supplied.
if type(random) is BuiltinMethod or type(getrandbits) is Method:
k = n.bit_length() # don't u (n-1) here becau n can be 1
微信动漫头像r = getrandbits(k) # 0 <= r < 2**k
while r >= n:
r = getrandbits(k)
return r
# There's an overridden random() method but no new getrandbits() method,
# so we can only u random() from here.
_warn("Underlying random() generator does not supply \n"
"enough bits to choo from a population range this large.\n"
"To remove the range limitation, add a getrandbits() method.")
return int(random() * n)
rem = maxsize % n
limit = (maxsize - rem) / maxsize # int(limit * maxsize) % n == 0
r = random()
while r >= limit:
r = random()
return int(r*maxsize) % n
-------------------- quence methods -------------------
def choice(lf, q):
"""Choo a random element from a non-empty quence."""
try:
i = lf._randbelow(len(q))
except ValueError:
rai IndexError('Cannot choo from an empty quence') from None return q[i]
def shuffle(lf, x, random=None):
"""Shuffle list x in place, and return None.
Optional argument random is a 0-argument function returning a random float in [0.0, 1.0); if it is the default None, the
standard random.random will be ud.
"""
if random is None:
randbelow = lf._randbelow
for i in reverd(range(1, len(x))):
# pick an element in x[:i+1] with which to exchange x[i]
j = randbelow(i+1)
x[i], x[j] = x[j], x[i]
el:
_int = int
for i in reverd(range(1, len(x))):