Bimodal distribution in C or Python

2019-07-18 02:54发布

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 transform, but if there's a ready-to-use library, or a simpler algorithm I don't know about, that'd be better.

标签: python c random
2条回答
淡お忘
2楼-- · 2019-07-18 03:32

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 ;-).

查看更多
三岁会撩人
3楼-- · 2019-07-18 03:46

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:
        return random.triangular( low1, high1, mode1 ) 
    else:
        return random.triangular( low2, high2, mode2 )

This may do everything you need.

查看更多
登录 后发表回答