home *** CD-ROM | disk | FTP | other *** search
/ MacHack 2000 / MacHack 2000.toast / pc / The Hacks / MacHacksBug / Python 1.5.2c1 / Extensions / Numerical / Lib / RandomArray.py < prev    next >
Encoding:
Python Source  |  2000-06-23  |  2.8 KB  |  96 lines

  1. import ranlib
  2. import Numeric
  3. import sys
  4. import math
  5. from types import *
  6.  
  7. ArgumentError = "ArgumentError"
  8.  
  9. def seed(x=0,y=0):
  10.     """seed(x, y), set the seed using the integers x, y; 
  11.     Set a random one from clock if  y == 0
  12.     """
  13.     if type (x) != IntType or type (y) != IntType :
  14.         raise ArgumentError, "seed requires integer arguments."
  15.     if y == 0:
  16.         import time
  17.         t = time.time()
  18.         ndigits = int(math.log10(t))
  19.         base = 10**(ndigits/2)
  20.         x = int(t/base)
  21.         y = 1 + int(t%base)
  22.     ranlib.set_seeds(x,y)
  23.  
  24. seed()
  25.  
  26. def get_seed():
  27.     "Return the current seed pair"
  28.     return ranlib.get_seeds()
  29.  
  30. def random(shape=[]):
  31.     "random(n) or random([n, m, ...]) returns array of random numbers"
  32.     if type(shape) == type(0): shape = [shape]
  33.     n = Numeric.multiply.reduce(shape)
  34.     s = ranlib.sample(n)
  35.     if len(shape) != 0:
  36.         return Numeric.reshape(s, shape)
  37.     else:
  38.         return s[0]
  39.  
  40. def uniform(minimum, maximum, shape=[]):
  41.     """uniform(minimum, maximum, shape=[]) returns array of given shape of random reals 
  42.     in given range"""
  43.     return minimum + (maximum-minimum)*random(shape)
  44.  
  45. def randint(minimum, maximum=None, shape=[]):
  46.     """randint(min, max, shape=[]) = random integers >=min, < max
  47.     If max not given, random integers >= 0, <min"""
  48.     if maximum == None:
  49.         maximum = minimum
  50.         minimum = 0
  51.     a = Numeric.asarray(uniform(minimum, maximum, shape)).astype(Numeric.Int)
  52.     if len(a.shape) == 0:
  53.         return a[0]
  54.     return a
  55.      
  56. def random_integers(maximum, minimum=1, shape=[]):
  57.     """random_integers(max, min=1, shape=[]) = random integers in range min-max inclusive"""
  58.     a = Numeric.asarray(uniform(minimum, maximum+1, shape)).astype(Numeric.Int)
  59.     if len(a.shape) == 0:
  60.         return a[0]
  61.     return a
  62.      
  63. def permutation(n):
  64.     "permutation(n) = a permutation of indices range(n)"
  65.     return Numeric.argsort(random(n))
  66.  
  67. def test():
  68.     x, y = get_seed()
  69.     print "Initial seed", x, y
  70.     seed(x, y)
  71.     x1, y1 = get_seed()
  72.     if x1 != x or y1 != y:
  73.         raise SystemExit, "Failed seed test."
  74.     print "First random number is", random()
  75.     print "Average of 10000 random numbers is", Numeric.sum(random(10000))/10000.
  76.     x = random([10,1000])
  77.     if len(x.shape) != 2 or x.shape[0] != 10 or x.shape[1] != 1000:
  78.         raise SystemExit, "random returned wrong shape"
  79.     x.shape = (10000,)
  80.     print "Average of 100 by 100 random numbers is", Numeric.sum(x)/10000.
  81.     y = uniform(0.5,0.6, (1000,10))
  82.     if len(y.shape) !=2 or y.shape[0] != 1000 or y.shape[1] != 10:
  83.         raise SystemExit, "uniform returned wrong shape"
  84.     y.shape = (10000,)
  85.     if Numeric.minimum.reduce(y) <= 0.5 or Numeric.maximum.reduce(y) >= 0.6:
  86.         raise SystemExit, "uniform returned out of desired range"
  87.     print "randint(1, 10, shape=[50])"
  88.     print randint(1, 10, shape=[50])
  89.     print "permutation(10)", permutation(10)
  90.     print "randint(3,9)", randint(3,9)
  91.     print "random_integers(10, shape=[20])"
  92.     print random_integers(10, shape=[20])
  93.  
  94. if __name__ == '__main__': 
  95.     test()
  96.