"Fossies" - the Fresh Open Source Software Archive

Member "NZMATH-1.2.0/nzmath/bigrandom.py" (19 Nov 2012, 2457 Bytes) of package /linux/misc/old/NZMATH-1.2.0.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Python source code syntax highlighting (style: standard) with prefixed line numbers. Alternatively you can here view or download the uninterpreted source code file. For more information about "bigrandom.py" see the Fossies "Dox" file reference documentation.

    1 #bigrandom.py
    2 
    3 import sys
    4 import random as _random
    5 import nzmath.arith1 as arith1
    6 
    7 
    8 def randrange(start, stop=None, step=1):
    9     """
   10     Choose a random item from range([start,] stop[, step]).
   11     (Return long integer.)
   12 
   13     see random.randrange
   14     """
   15     if stop is None:
   16         if abs(start) < sys.maxint:
   17             return _random.randrange(start)
   18     elif abs(stop - start) < sys.maxint:
   19         return _random.randrange(start, stop, step)
   20 
   21     negative_step = False
   22     if stop is None:
   23         start, stop = 0, start
   24     if step < 0:
   25         start, stop, step = -start, -stop, -step
   26         negative_step = True
   27 
   28     _validate_for_randrange(start, stop, step)
   29 
   30     if (stop - start) % step != 0:
   31         v = (stop - start)//step + 1
   32     else:
   33         v = (stop - start)//step
   34 
   35     bitlength = arith1.log(v, base=2)
   36     if v != 2**bitlength:
   37         bitlength += 1
   38     randomized = v + 1 # to pass the first test
   39     while randomized >= v:
   40         randomized = 0
   41         for i in range(bitlength):
   42             if random() < 0.5:
   43                 randomized += 1 << i
   44     result = randomized * step + start
   45     if negative_step:
   46         return -result
   47     return result
   48 
   49 def _validate_for_randrange(start, stop, step):
   50     """
   51     Check validity of arguments for randrange.
   52     """
   53     if step == 0:
   54         raise ValueError("zero step for randrange()")
   55     elif start != long(start):
   56         raise ValueError("non-integer arg 1 for randrange()")
   57     elif stop != long(stop):
   58         raise ValueError("non-integer stop for randrange()")
   59     elif step != long(step):
   60         raise ValueError("non-integer step for randrange()")
   61     if start >= stop:
   62         raise ValueError("empty range for randrange()")
   63 
   64 random = _random.random
   65 seed = _random.seed
   66 
   67 
   68 def map_choice(mapping, upperbound):
   69     """
   70     Return a choice from a set given as the image of the mapping from
   71     natural numbers (more precisely range(upperbound)).  In other
   72     words, it is equivalent to
   73     random.choice([mapping(i) for i in range(upperboud) if mapping(i) != None])
   74     if upperbound is small enough for the list size limit.
   75 
   76     The mapping can be a partial function, i.e. it may return None for
   77     some input.  However, if the resulting set is empty, it will end
   78     up with an infinite loop.
   79     """
   80     result = None
   81     while result is None:
   82         result = mapping(randrange(upperbound))
   83     return result
   84 
   85 
   86 __all__ = ['random', 'randrange', 'seed', 'map_choice']