import ranlib import Numeric import sys import math from types import * ArgumentError = "ArgumentError" def seed(x=0,y=0): """seed(x, y), set the seed using the integers x, y; Set a random one from clock if y == 0 """ if type (x) != IntType or type (y) != IntType : raise ArgumentError, "seed requires integer arguments." if y == 0: import time t = time.time() ndigits = int(math.log10(t)) base = 10**(ndigits/2) x = int(t/base) y = 1 + int(t%base) ranlib.set_seeds(x,y) seed() def get_seed(): "Return the current seed pair" return ranlib.get_seeds() def random(shape=[]): "random(n) or random([n, m, ...]) returns array of random numbers" if type(shape) == type(0): shape = [shape] n = Numeric.multiply.reduce(shape) s = ranlib.sample(n) if len(shape) != 0: return Numeric.reshape(s, shape) else: return s[0] def uniform(minimum, maximum, shape=[]): """uniform(minimum, maximum, shape=[]) returns array of given shape of random reals in given range""" return minimum + (maximum-minimum)*random(shape) def randint(minimum, maximum=None, shape=[]): """randint(min, max, shape=[]) = random integers >=min, < max If max not given, random integers >= 0, = 0.6: raise SystemExit, "uniform returned out of desired range" print "randint(1, 10, shape=[50])" print randint(1, 10, shape=[50]) print "permutation(10)", permutation(10) print "randint(3,9)", randint(3,9) print "random_integers(10, shape=[20])" print random_integers(10, shape=[20]) if __name__ == '__main__': test()