File: prng.py

package info (click to toggle)
python-scipy 0.6.0-12
  • links: PTS, VCS
  • area: main
  • in suites: lenny
  • size: 32,016 kB
  • ctags: 46,675
  • sloc: cpp: 124,854; ansic: 110,614; python: 108,664; fortran: 76,260; objc: 424; makefile: 384; sh: 10
file content (36 lines) | stat: -rw-r--r-- 1,120 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
""" Package-global pseudo-random number generator.

This global is a transitional hack from the old code. Ideally, each run of a GA
should control its own in order to allow multiple concurrent runs. However, we
are transitioning from an older implementation that used a really global PRNG.
"""

from numpy.random import RandomState


class GAPRNG(RandomState):
    """ PRNG for the GA package.

    In addition to all of the functionality derived from RandomState, we also
    store the seed values that were used.
    """

    def seed(self, seed=None):
        """ Seed the generator.

        seed can be an integer, an array (or other sequence) of integers of any
        length, or None. If seed is None, then RandomState will try to read data
        from /dev/urandom (or the Windows analogue) if available or seed from
        the clock otherwise.
        """
        RandomState.seed(self, seed)
        self.initial_seed = seed

    def choice(self, seq):
        """ Randomly and uniformly select an item from a sequence.
        """
        i = self.randint(len(seq))
        return seq[i]


prng = GAPRNG()