Top Previous Next

7. Python implementation: module crng

A number of different RNGs, and different distributions, have been implemented as a Python module containing so-called extension types coded in C.

The crng module can be obtained at this location.

Python interactive usage:

    % python
    >>> import crng
    >>> r = crng.ParkMiller(seed=123)
    >>> print r()
    0.000962643418909
    >>> print r(2)
    (0.179147941609, 0.939454629989)

To use a another distribution, create it with the help of a basic uniform (0,1) RNG:

    >>> n=crng.NormalDeviate(rng=crng.Ranlux(), stdev=4.0)
    >>> n(4)
    (0.960612428619, 6.30989834512, -0.972826635646, -2.09557141754)

There are a few utility procedures for sampling and shuffling:

    >>> r = crng.ParkMiller(seed=123)
    >>> seq = "abcdefghijklmnopqrstuvwxyz"
    >>> seq2 = crng.Shuffle(seq, r)
    >>> print seq2
    yzmvrohkifbgduncqsxptljwea

Copyright © 2000 Per Kraulis $Date: 2000/12/12 15:59:07 $