产生在C特定分布的随机数 [英] generating random number with a specific distribution in c

查看:135
本文介绍了产生在C特定分布的随机数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要用于产生随机数,给定的平均,标准偏差,并用三个分布的一个功能库 - 指数的,正常的或统一的。

i need a library with functions for generating random number, given average, standard deviation and using one of three distribution - exponential, normal or unified.

三连的人会有所帮助。
我在找这样的事情 - HTTP://www.$c$ cproject.com/KB/recipes/zigurat.aspx ,但在C

even one of the three would help. i'm looking for something like this - http://www.codeproject.com/KB/recipes/zigurat.aspx, but in c.

感谢

推荐答案

统一的:
结果,生成的范围内的随机数[0,1]与<一href=\"http://stackoverflow.com/questions/1340729/how-do-you-generate-a-random-double-uniformly-distributed-between-0-and-1-from-c\">uniform分布:

double X=((double)rand()/(double)RAND_MAX);

Exponentional
结果产生<一个href=\"http://stackoverflow.com/questions/6527345/simulating-poisson-waiting-times-java/6527433#6527433\">exponentional随机变量带参数的lambda

Exponentional
generating an exponentional random variable with parameter lambda:

-ln(U)/lambda (where U~Uniform[0,1]). 

正常:
结果最简单的方法[虽然耗时]使用中心极限定理,[总和够均匀分布的数字],但也有在维基百科页面其他方法如的箱穆勒变换产生2个独立随机变量:X,Y〜N(0, 1)

normal:
the simplest way [though time consuming] is using the central limit theorem, [sum enough uniformly distributed numbers] but there are other methods in the wikipedia page such as the box muller transform that generates 2 independent random variables: X,Y~N(0,1)

X=sqrt(-2ln(U))*cos(2*pi*V)
Y=sqrt(-2ln(U))*sin(2*pi*V)
where U,V~UNIFORM[0,1]

从X〜N(0,1)到Z〜N转化(M,S ^ 2)很简单:以Z = S * X + M

虽然可以生成这些随机数字,我站在@Amigable克拉克康德建议使用现有的库。

Though you CAN generate these random numbers, I stand by @Amigable Clark Kant suggestion to use an existing library.

这篇关于产生在C特定分布的随机数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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