SF #1027105: HardwareRandom should be renamed OSRandom

Renamed the new generator at Trevor's recommendation.
The name HardwareRandom suggested a bit more than it
delivered (no radioactive decay detectors or such).
This commit is contained in:
Raymond Hettinger 2004-09-13 22:23:21 +00:00
parent 3e773fb622
commit 23f1241dc6
3 changed files with 24 additions and 20 deletions

View file

@ -61,10 +61,11 @@ Bookkeeping functions:
Optional argument \var{x} can be any hashable object. Optional argument \var{x} can be any hashable object.
If \var{x} is omitted or \code{None}, current system time is used; If \var{x} is omitted or \code{None}, current system time is used;
current system time is also used to initialize the generator when the current system time is also used to initialize the generator when the
module is first imported. If hardware random sources are available, module is first imported. If randomness sources are provided by the
they are used instead of the system time (see the \function{os.urandom()} operating system, they are used instead of the system time (see the
\function{os.urandom()}
function for details on availability). \versionchanged[formerly, function for details on availability). \versionchanged[formerly,
hardward sources were not used]{2.4} operating system resources were not used]{2.4}
If \var{x} is not \code{None} or an int or long, If \var{x} is not \code{None} or an int or long,
\code{hash(\var{x})} is used instead. \code{hash(\var{x})} is used instead.
If \var{x} is an int or long, \var{x} is used directly. If \var{x} is an int or long, \var{x} is used directly.
@ -249,9 +250,10 @@ require care that two independent random sequences do not overlap.
yield no more than about 2**24 distinct internal states in all. yield no more than about 2**24 distinct internal states in all.
\end{funcdesc} \end{funcdesc}
\begin{classdesc}{HardwareRandom}{\optional{seed}} \begin{classdesc}{SystemRandom}{\optional{seed}}
Class that uses the \function{os.urandom()} function for generating Class that uses the \function{os.urandom()} function for generating
random numbers from hardware. Not available on all systems. random numbers from sources provided by the operating system.
Not available on all systems.
Does not rely on software state and sequences are not reproducible. Does not rely on software state and sequences are not reproducible.
Accordingly, the \method{seed()} and \method{jumpahead()} methods Accordingly, the \method{seed()} and \method{jumpahead()} methods
have no effect and are ignored. The \method{getstate()} and have no effect and are ignored. The \method{getstate()} and

View file

@ -52,7 +52,7 @@ __all__ = ["Random","seed","random","uniform","randint","choice","sample",
"expovariate","vonmisesvariate","gammavariate", "expovariate","vonmisesvariate","gammavariate",
"gauss","betavariate","paretovariate","weibullvariate", "gauss","betavariate","paretovariate","weibullvariate",
"getstate","setstate","jumpahead", "WichmannHill", "getrandbits", "getstate","setstate","jumpahead", "WichmannHill", "getrandbits",
"HardwareRandom"] "SystemRandom"]
NV_MAGICCONST = 4 * _exp(-0.5)/_sqrt(2.0) NV_MAGICCONST = 4 * _exp(-0.5)/_sqrt(2.0)
TWOPI = 2.0*_pi TWOPI = 2.0*_pi
@ -99,8 +99,8 @@ class Random(_random.Random):
def seed(self, a=None): def seed(self, a=None):
"""Initialize internal state from hashable object. """Initialize internal state from hashable object.
None or no argument seeds from current time or from a hardware None or no argument seeds from current time or from an operating
randomness source if available. system specific randomness source if available.
If a is not None or an int or long, hash(a) is used instead. If a is not None or an int or long, hash(a) is used instead.
""" """
@ -603,8 +603,8 @@ class WichmannHill(Random):
def seed(self, a=None): def seed(self, a=None):
"""Initialize internal state from hashable object. """Initialize internal state from hashable object.
None or no argument seeds from current time or from a hardware None or no argument seeds from current time or from an operating
randomness source if available. system specific randomness source if available.
If a is not None or an int or long, hash(a) is used instead. If a is not None or an int or long, hash(a) is used instead.
@ -744,10 +744,12 @@ class WichmannHill(Random):
z = (z + a) % 256 or 1 z = (z + a) % 256 or 1
self.__whseed(x, y, z) self.__whseed(x, y, z)
## -------------------- Hardware Random Source ------------------- ## --------------- Operating System Random Source ------------------
class HardwareRandom(Random): class SystemRandom(Random):
"""Alternate random number generator using hardware sources. """Alternate random number generator using sources provided
by the operating system (such as /dev/urandom on Unix or
CryptGenRandom on Windows).
Not available on all systems (see os.urandom() for details). Not available on all systems (see os.urandom() for details).
""" """
@ -767,13 +769,13 @@ class HardwareRandom(Random):
return x >> (bytes * 8 - k) # trim excess bits return x >> (bytes * 8 - k) # trim excess bits
def _stub(self, *args, **kwds): def _stub(self, *args, **kwds):
"Stub method. Not used for a hardware random number generator." "Stub method. Not used for a system random number generator."
return None return None
seed = jumpahead = _stub seed = jumpahead = _stub
def _notimplemented(self, *args, **kwds): def _notimplemented(self, *args, **kwds):
"Method should not be called for a hardware random number generator." "Method should not be called for a system random number generator."
raise NotImplementedError('Hardware entropy source does not have state.') raise NotImplementedError('System entropy source does not have state.')
getstate = setstate = _notimplemented getstate = setstate = _notimplemented
## -------------------- test program -------------------- ## -------------------- test program --------------------

View file

@ -164,8 +164,8 @@ class WichmannHill_TestBasicOps(TestBasicOps):
self.assertRaises(UserWarning, self.gen.randrange, 2**60) self.assertRaises(UserWarning, self.gen.randrange, 2**60)
warnings.filters[:] = oldfilters warnings.filters[:] = oldfilters
class HardwareRandom_TestBasicOps(TestBasicOps): class SystemRandom_TestBasicOps(TestBasicOps):
gen = random.HardwareRandom() gen = random.SystemRandom()
def test_autoseed(self): def test_autoseed(self):
# Doesn't need to do anything except not fail # Doesn't need to do anything except not fail
@ -496,11 +496,11 @@ def test_main(verbose=None):
TestModule] TestModule]
try: try:
random.HardwareRandom().random() random.SystemRandom().random()
except NotImplementedError: except NotImplementedError:
pass pass
else: else:
testclasses.append(HardwareRandom_TestBasicOps) testclasses.append(SystemRandom_TestBasicOps)
test_support.run_unittest(*testclasses) test_support.run_unittest(*testclasses)