Bimodal distribution in C or Python

自闭症网瘾萝莉.ら 提交于 2020-01-15 03:15:06

问题


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.


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

This may do everything you need.




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



来源:https://stackoverflow.com/questions/651421/bimodal-distribution-in-c-or-python

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!