Bimodal Distribution In C Or Python
What's the easiest way to generate random values according to a bimodal distribution in C or Python? I could implement something like the Ziggurat algorithm or a Box-Muller transfo
Solution 1:
Aren't you just picking values either of two modal distributions?
http://docs.python.org/library/random.html#random.triangular
Sounds like you just toggle back and forth between two sets of parameters for your call to triangular.
def bimodal( low1, high1, mode1, low2, high2, mode2 ):
toss = random.choice( (1, 2) )
if toss == 1:
returnrandom.triangular( low1, high1, mode1 )
else:
returnrandom.triangular( low2, high2, mode2 )
This may do everything you need.
Solution 2:
There's always the old-fashioned straight-forward accept-reject algorithm. If it was good enough for Johnny von Neumann it should be good enough for you ;-).
Post a Comment for "Bimodal Distribution In C Or Python"