用C或Python双峰分布 [英] Bimodal distribution in C or Python

查看:119
本文介绍了用C或Python双峰分布的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

推荐答案

你是不是刚刚好转值以下两种模态分布的?

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.

这篇关于用C或Python双峰分布的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆