[PYTHON MATRIX-SIG] Ranf.py for 1.0alpha1
Janne Sinkkonen
janne@avocado.pc.helsinki.fi
11 Aug 1996 18:10:58 +0300
... after one or two trivial fixes:
import URNG
import Numeric
# 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():
"ranf() = a random number from the standard generator."
return standard_generator.ranf()
def random_sample(*n):
"""random_sample(n) = array of n random numbers;
random_sample(n1, n2, ...)= random array of shape (n1, n2, ..)"""
if not n:
return standard_generator.sample(1)
m = 1
for i in n:
m = m * i
return Numeric.reshape(standard_generator.sample(m), n)
--
Janne Sinkkonen <janne@iki.fi> <URL: http://www.iki.fi/~janne/ >
=================
MATRIX-SIG - SIG on Matrix Math for Python
send messages to: matrix-sig@python.org
administrivia to: matrix-sig-request@python.org
=================