根据分布生成随机数 [英] Generate random numbers according to distributions

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

问题描述

我想根据某些分布生成随机数.我该怎么办?

I want to generate random numbers according some distributions. How can I do this?

推荐答案

您获得的标准随机数生成器(经过简单转换后在C中为rand(),在许多语言中均等效)非常近似于统一分布在[0,1]范围内.如果这是您所需要的,那就完成了.将其转换为在更大的整数范围内生成的随机数也很简单.

The standard random number generator you've got (rand() in C after a simple transformation, equivalents in many languages) is a fairly good approximation to a uniform distribution over the range [0,1]. If that's what you need, you're done. It's also trivial to convert that to a random number generated over a somewhat larger integer range.

已经将统一分布转换为正态分布在SO 上进行操作,就像转到指数分布一样.

Conversion of a Uniform distribution to a Normal distribution has already been covered on SO, as has going to the Exponential distribution.

:对于三角分布,转换统一变量相对简单(在类似C的东西):

: For the triangular distribution, converting a uniform variable is relatively simple (in something C-like):

double triangular(double a,double b,double c) {
   double U = rand() / (double) RAND_MAX;
   double F = (c - a) / (b - a);
   if (U <= F)
      return a + sqrt(U * (b - a) * (c - a));
   else
      return b - sqrt((1 - U) * (b - a) * (b - c));
}

那只是转换Wikipedia页面上给出的公式.如果您想要其他人,那么这就是开始寻找的地方;通常,您可以使用统一变量在累积密度函数的垂直轴上选择一个点(假设它是连续的),然后反转CDF以获得具有所需分布的随机值.

That's just converting the formula given on the Wikipedia page. If you want others, that's the place to start looking; in general, you use the uniform variable to pick a point on the vertical axis of the cumulative density function of the distribution you want (assuming it's continuous), and invert the CDF to get the random value with the desired distribution.

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

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