Entry
Problems with frand()
Jul 5th, 2000 09:59
Nathan Wallace, Hans Nowak, Snippet 29, Daniel T.
"""
Packages: maths.random
"""
"""
With this post, I would like to do two things. A) Get critique on whether
I did well on my first Python program and B) see if someone can explain
why frand(0) never seems to return 0?
"""
# This code is a Python version of frand.c found in
# snip 9707
# The origional code was written by Larry Hudson
# Return random number in range (0.0, 1.0] #
# #
# If n is negative it will randomize the seed, based on the #
# current time. #
# If n is zero it will return the next random number. #
# If n is positive it will set the seed to a value based on #
# the value of n. #
from time import clock
TEN_PI = 31.41592653589793
E = 2.718281828459045
seed = E
def frand(n):
" returns random number in range (0.0, 1.0]"
global seed
if n < 0:
seed = clock() * E
elif n > 0:
seed = n * E
else:
seed = seed * TEN_PI + E
seed = seed % 1
return seed
###########################
lowest = 1
highest = 0
frand(-1)
for x in range(100000):
y = frand(0)
if y < lowest:
lowest = y
if y > highest:
highest = y
print lowest, highest