[PYTHON MATRIX-SIG] Uniform Random Number Generator extension class

P. Dubois dubois@kristen.llnl.gov
Mon, 20 Nov 1995 10:18:38 -0800

On the matrix extension site I have posted URNG.tar. It has been tested with
the 0.11 version of the matrix extension.

This class supplies two modules:

   Uniform random number generator objects can be created with either
a seed initialized from the clock or a seed supplied by the creating call.
The random number generator objects contain methods to return either a single
random number or a vector of random numbers (i.e., a Matrix of shape (n,)).
The individual generators are completely independent and do not affect the
stream produced by other generators. The generator is a Cray ranf() 
compatible generator.

   This is the interface intended for users. Here is the entire text:

import URNG
import Matrix
# x=CreateGenerator(seed) creates an random number generator stream
#   seed < 0  ==>  Use the default initial seed value.
#   seed = 0  ==>  Set a "random" value for the seed from the system clock.
#   seed > 0  ==>  Set seed directly (32 bits only).
#   x.ranf() samples from that stream.
#   x.sample(n) returns a vector from that stream.
# ranf() returns a stream of random numbers
# random_sample(n) returns a vector of length n filled with random numbers
# random_matrix(n,m,...) returns an arbitrarily shaped matrix filled with
#                        random numbers.

CreateGenerator = URNG.CreateGenerator
standard_generator = CreateGenerator(-1)

def ranf():
	"Returns a single random number from the standard generator."
	return standard_generator.ranf()

def random_sample(n):
	"""Returns a vector of length n of random numbers 
	from the standard generator."""
	return  standard_generator.sample(n)

def random_matrix(*n):
	"""Returns a matrix with shape given by n of random numbers from 
	the standard generator."""
	if not n:
		raise 'random_matrix cannot be an empty shape'
        m = 1
	for i in n:
		m = m * i
	x = Matrix.reshape(random_sample(m),n)
        return x

MATRIX-SIG  - SIG on Matrix Math for Python

send messages to: matrix-sig@python.org
administrivia to: matrix-sig-request@python.org