import RandomNumberGenerator
import math
class RandBox:
""" generator for gaussian deviates (Box-Mueller) """
def __init__(self):
self.__random = \ RandomNumberGenerator.defaultInstance()
self.__available = 0
def next(self):
""" move to the next element of the pseudo random
sequence """
# moving to the next element either uses up one
# deviate or triggers the generation of two new
# deviates
if self.__available:
self.__available = 0
else:
self.__generate()
def lastGaussian(self):
""" pseudo random deviate from the
gaussian distribution """
if self.__available:
return self.__first
else:
return self.__second
def __generate(self):
""" generate two pseudo random deviates from a
gaussian distribution """
self.__randpoint()
p = math.sqrt(((-2.0 * math.log(self.__product)/\ self.__product)))
self.__available = 1
self.__first = p * self.__x
self.__second = p * self.__y
def __randpoint(self):
""" a point from a uniform distribution on the
area of the unit circle """
self.__product = 2
while self.__product > 1:
self.__x = self.__randuniv() * 2.0 - 1.0
self.__y = self.__randuniv() * 2.0 - 1.0
self.__product = self.__x * self.__x + \ self.__y * self.__y
__x = 0.0
__y = 0.0
__product = 0.0
__first = 0.0
__second = 0.0
__available = 0
__random = 0
def __randuniv(self):
""" internal wrapper for uniform distribution on
unit interval """
self.__random.next()
return self.__random.lastUniform() |