Back to index

python3.2  3.2.2
Public Member Functions | Public Attributes | Static Public Attributes
random.Random Class Reference
Inheritance diagram for random.Random:
Inheritance graph
[legend]

List of all members.

Public Member Functions

def __init__
def seed
def getstate
def setstate

Public Attributes

 gauss_next

Static Public Attributes

int VERSION = 3

Detailed Description

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 following
methods:  random(), seed(), getstate(), and setstate().
Optionally, implement a getrandbits() method so that randrange()
can cover arbitrarily large ranges.

Definition at line 69 of file random.py.


Constructor & Destructor Documentation

def random.Random.__init__ (   self,
  x = None 
)
Initialize an instance.

Optional argument x controls seeding, as for Random.seed().

Definition at line 85 of file random.py.

00085 
00086     def __init__(self, x=None):
00087         """Initialize an instance.
00088 
00089         Optional argument x controls seeding, as for Random.seed().
00090         """
00091 
00092         self.seed(x)
00093         self.gauss_next = None

Here is the call graph for this function:

Here is the caller graph for this function:


Member Function Documentation

def random.Random.getstate (   self)
Return internal state; can be passed to setstate() later.

Definition at line 124 of file random.py.

00124 
00125     def getstate(self):
00126         """Return internal state; can be passed to setstate() later."""
00127         return self.VERSION, super().getstate(), self.gauss_next

def random.Random.seed (   self,
  a = None,
  version = 2 
)
Initialize internal state from hashable object.

None or no argument seeds from current time or from an operating
system specific randomness source if available.

For version 2 (the default), all of the bits are used if *a *is a str,
bytes, or bytearray.  For version 1, the hash() of *a* is used instead.

If *a* is an int, all bits are used.

Reimplemented in random.SystemRandom.

Definition at line 94 of file random.py.

00094 
00095     def seed(self, a=None, version=2):
00096         """Initialize internal state from hashable object.
00097 
00098         None or no argument seeds from current time or from an operating
00099         system specific randomness source if available.
00100 
00101         For version 2 (the default), all of the bits are used if *a *is a str,
00102         bytes, or bytearray.  For version 1, the hash() of *a* is used instead.
00103 
00104         If *a* is an int, all bits are used.
00105 
00106         """
00107 
00108         if a is None:
00109             try:
00110                 a = int.from_bytes(_urandom(32), 'big')
00111             except NotImplementedError:
00112                 import time
00113                 a = int(time.time() * 256) # use fractional seconds
00114 
00115         if version == 2:
00116             if isinstance(a, (str, bytes, bytearray)):
00117                 if isinstance(a, str):
00118                     a = a.encode()
00119                 a += _sha512(a).digest()
00120                 a = int.from_bytes(a, 'big')
00121 
00122         super().seed(a)
00123         self.gauss_next = None

Here is the caller graph for this function:

def random.Random.setstate (   self,
  state 
)
Restore internal state from object returned by getstate().

Definition at line 128 of file random.py.

00128 
00129     def setstate(self, state):
00130         """Restore internal state from object returned by getstate()."""
00131         version = state[0]
00132         if version == 3:
00133             version, internalstate, self.gauss_next = state
00134             super().setstate(internalstate)
00135         elif version == 2:
00136             version, internalstate, self.gauss_next = state
00137             # In version 2, the state was saved as signed ints, which causes
00138             #   inconsistencies between 32/64-bit systems. The state is
00139             #   really unsigned 32-bit ints, so we convert negative ints from
00140             #   version 2 to positive longs for version 3.
00141             try:
00142                 internalstate = tuple(x % (2**32) for x in internalstate)
00143             except ValueError as e:
00144                 raise TypeError from e
00145             super().setstate(internalstate)
00146         else:
00147             raise ValueError("state with version %s passed to "
00148                              "Random.setstate() of version %s" %
00149                              (version, self.VERSION))


Member Data Documentation

Definition at line 92 of file random.py.

int random.Random.VERSION = 3 [static]

Definition at line 83 of file random.py.


The documentation for this class was generated from the following file: