Back to index

python3.2  3.2.2
Public Member Functions | Public Attributes | Static Public Attributes | Private Member Functions
random.SystemRandom Class Reference

--------------- Operating System Random Source ------------------ More...

Inheritance diagram for random.SystemRandom:
Inheritance graph
Collaboration diagram for random.SystemRandom:
Collaboration graph

List of all members.

Public Member Functions

def random
def getrandbits
def seed
def getstate
def setstate

Public Attributes


Static Public Attributes

 getstate = setstate_notimplemented
int VERSION = 3

Private Member Functions

def _notimplemented

Detailed Description

--------------- Operating System Random Source ------------------

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).

Definition at line 626 of file

Member Function Documentation

def random.SystemRandom._notimplemented (   self,
) [private]

Definition at line 652 of file

00653     def _notimplemented(self, *args, **kwds):
00654         "Method should not be called for a system random number generator."
        raise NotImplementedError('System entropy source does not have state.')
def random.SystemRandom.getrandbits (   self,
getrandbits(k) -> x.  Generates a long int with k random bits.

Definition at line 638 of file

00639     def getrandbits(self, k):
00640         """getrandbits(k) -> x.  Generates a long int with k random bits."""
00641         if k <= 0:
00642             raise ValueError('number of bits must be greater than zero')
00643         if k != int(k):
00644             raise TypeError('number of bits should be an integer')
00645         numbytes = (k + 7) // 8                       # bits / 8 and rounded up
00646         x = int.from_bytes(_urandom(numbytes), 'big')
00647         return x >> (numbytes * 8 - k)                # trim excess bits

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

Definition at line 124 of file

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

Get the next random number in the range [0.0, 1.0).

Definition at line 634 of file

00635     def random(self):
00636         """Get the next random number in the range [0.0, 1.0)."""
00637         return (int.from_bytes(_urandom(7), 'big') >> 3) * RECIP_BPF

def random.SystemRandom.seed (   self,
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 from random.Random.

Definition at line 648 of file

00649     def seed(self, *args, **kwds):
00650         "Stub method.  Not used for a system random number generator."
00651         return None

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

Definition at line 128 of file

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.SystemRandom.getstate = setstate_notimplemented [static]

Definition at line 655 of file

int random.Random.VERSION = 3 [static, inherited]

Definition at line 83 of file

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