4Suite™ API Documentation

# ► Module Ft.Lib.Random

Thread-safe random number generation

    Random number generation capabilities, speed, and thread safety in
    stdlib vary from version to version of Python. In addition, attempts to
    use an OS-specific random number source can result in unexpected
    exceptions being raised. Also, a bug in Python 2.3.0 can lead to a
    reduction in entropy, and a bug in Python 2.4.0 and 2.4.1 can result
    in exceptions related to open filehandles on some multithreaded Posix
    platforms.

    This module works around as many of these issues as it can by defining
    random number generator classes that can be used safely by multiple
    threads, using the best random number sources available. They support
    all versions of Python from 2.1 up, and fall back on more reliable
    generators when exception conditions occur. In addition, convenience
    functions equivalent to random.random() and os.urandom() are exposed.

    Copyright 2006 Fourthought, Inc. (USA).
    Detailed license and copyright information: http://4suite.org/COPYRIGHT
    Project home, documentation, distributions: http://4suite.org/

Classes: FtRandom, FtSystemRandom |  |  |  | Functions: GetRandomBytes, Random, urandom |  |  |  | Globals: DEFAULT_RNG

## Classes

class FtRandom(random.Random, object)
  The best available OS-agnostic PRNG, thread-safe.

      Implements getrandbits() in all versions of Python.
      Also adds getrandbytes(), which returns a str of bytes.

  ### Methods

  __init__(self, *args, **kwargs)
    Overrides: __init__ from class Random
  gauss = _gauss(self, *args, **kwargs)
    Gaussian distribution.

        mu is the mean, and sigma is the standard deviation.

        Thread-safe.

    Overrides: gauss from class Random
  getrandbytes = _getrandbytes(self, k)
    getrandbytes(k) -> x. Returns k random bytes as a str.
  seed(self, a=None)
    Initialize internal state from hashable object.

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

        If a is not None or an int or long, hash(a) is used instead.

    Overrides: seed from class Random

  ### Methods inherited from class random.Random

  __getstate__, __reduce__, __setstate__, betavariate, choice, expovariate, gammavariate, getstate, lognormvariate, normalvariate, paretovariate, randint, randrange, sample, setstate, shuffle, uniform, vonmisesvariate, weibullvariate

  ### Methods inherited from class _random.Random

  __getattribute__, __new__, getrandbits, jumpahead, random

  ### Methods inherited from class object

  __delattr__, __hash__, __reduce_ex__, __repr__, __setattr__, __str__

  ### Members inherited from class random.Random

  VERSION, __dict__, __weakref__

  ### Members inherited from class object

  __class__
class FtSystemRandom(FtRandom)
  A PRNG that uses an OS-specific random number source, if available, falling back on an instance of FtRandom.

      Calls to seed(), jumpahead(), getstate() and setstate() only affect
      the fallback FtRandom instance.

      Implements getrandbits() in all versions of Python.
      Also adds getrandbytes(), which returns a str of bytes.

  ### Methods

  __init__(self, *args, **kwargs)
    Overrides: __init__ from class FtRandom
  getrandbits = _getrandbits(self, *args, **kwargs)
    getrandbits(k) -> x. Generates a long int with k random bits.
    Overrides: getrandbits from class FtRandom
  getrandbytes = _getrandbytes(self, k)
    getrandbytes(k) -> x. Returns k random bytes as a str.
    Overrides: getrandbytes from class FtRandom
  getstate(self)
    Return internal state; can be passed to setstate() later.
    Overrides: getstate from class FtRandom
  jumpahead(self, *args, **kwargs)
    Make the fallback PRNG (an instance of FtRandom) jump ahead
    Overrides: jumpahead from class FtRandom
  random = _random(self)
    Get the next random number in the range [0.0, 1.0).
    Overrides: random from class FtRandom
  seed(self, *args, **kwargs)
    Seed the fallback PRNG (an instance of FtRandom)
    Overrides: seed from class FtRandom
  setstate(self, state)
    Restore internal state from object returned by getstate().
    Overrides: setstate from class FtRandom

  ### Methods inherited from class FtRandom

  gauss

  ### Methods inherited from class random.Random

  __getstate__, __reduce__, __setstate__, betavariate, choice, expovariate, gammavariate, lognormvariate, normalvariate, paretovariate, randint, randrange, sample, shuffle, uniform, vonmisesvariate, weibullvariate

  ### Methods inherited from class _random.Random

  __getattribute__, __new__

  ### Methods inherited from class object

  __delattr__, __hash__, __reduce_ex__, __repr__, __setattr__, __str__

  ### Members inherited from class random.Random

  VERSION, __dict__, __weakref__

  ### Members inherited from class object

  __class__

## Functions

GetRandomBytes(numBytes)
  Returns a string of random bytes from the best RNG available. Equivalent to os.urandom(), but failsafe.
Random()
  Returns a random float, n, where 0 <= n < 1
urandom(n)
  urandom(n) -> str

      Return a string of n random bytes suitable for cryptographic use.

## Globals

DEFAULT_RNG = <Ft.Lib.Random.FtSystemRandom object>
  A PRNG that uses an OS-specific random number source, if available, falling back on an instance of FtRandom.

      Calls to seed(), jumpahead(), getstate() and setstate() only affect
      the fallback FtRandom instance.

      Implements getrandbits() in all versions of Python.
      Also adds getrandbytes(), which returns a str of bytes.