非均匀随机数生成器实现? [英] Non-Uniform Random Number Generator Implementation?

查看:190
本文介绍了非均匀随机数生成器实现?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一个随机数生成器,它以可编程的均值来选择指定范围内的数字.

I need a random number generator that picks numbers over a specified range with a programmable mean.

例如,我需要选择2到14之间的数字,并且我需要将随机数的平均值设为5.

For example, I need to pick numbers between 2 and 14 and I need the average of the random numbers to be 5.

我经常使用随机数生成器.通常我只需要一个均匀的分布.

I use random number generators a lot. Usually I just need a uniform distribution.

我什至不知道该怎么称呼这种分布.

I don't even know what to call this type of distribution.

感谢您提供的任何帮助或见识.

Thank you for any assistance or insight you can provide.

推荐答案

您也许可以使用二项式分布,如果您对这种分布的形状感到满意的话.设置n = 12和p = 0.25.这将为您提供0到12之间的值,平均值为3.只需将每个结果加2即可得到范围并表示您正在寻找.

You might be able to use a binomial distribution, if you're happy with the shape of that distribution. Set n=12 and p=0.25. This will give you a value between 0 and 12 with a mean of 3. Just add 2 to each result to get the range and mean you are looking for.

关于实施,您可能可以找到支持非均匀分布的所选语言的库(我已经用Java自己写了一个).

As for implementation, you can probably find a library for your chosen language that supports non-uniform distributions (I've written one myself for Java).

使用统一的RNG可以很容易地近似二项式分布.只需执行 n 次试验并记录成功次数.因此,如果您有n = 10和p = 0.5,就好比连续掷硬币10次并计算正面数.对于p = 0.25,只需生成0到3之间的均匀分布值,并且仅将零视为成功即可.

A binomial distribution can be approximated fairly easily using a uniform RNG. Simply perform n trials and record the number of successes. So if you have n=10 and p=0.5, it's just like flipping a coin 10 times in a row and counting the number of heads. For p=0.25 just generate uniformly-distributed values between 0 and 3 and only count zeros as successes.

如果想要更有效的实现,可以在Knuth的《计算机编程艺术》第2卷的练习中隐藏一个聪明的算法.

If you want a more efficient implementation, there is a clever algorithm hidden away in the exercises of volume 2 of Knuth's The Art of Computer Programming.

这篇关于非均匀随机数生成器实现?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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