为较低的数字生成具有较高概率的随机自然数? [英] Generating random natural numbers with higher probability for lower numbers?

查看:48
本文介绍了为较低的数字生成具有较高概率的随机自然数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找像 Python 的 random.randint() 这样的函数,它可以在 ab 之间生成随机整数,但它更有可能生成数字更接近于a,只有少数更接近于b.

I'm looking for a function like Python's random.randint() which generates random whole numbers between a and b, but one which is more likely to generate numbers closer to a, with only a few closer to b.

有这样的功能吗?

推荐答案

您的问题很模糊,因为有许多随机分布,其中较低的数字比较高的数字更有可能.另外,说在 ab 之间"这里同样含糊不清.这是许多示例之一,它以您要求的方式在闭区间 [a, b] 中生成一个随机整数:

Your question is vague as there are numerous random distributions in which lower numbers are more likely than higher numbers. Also, saying "between a and b" is likewise vague here. Here is one of many examples, which produces a random integer in the closed interval [a, b] in the manner you're asking for:

min(random.randint(a, b), random.randint(a, b))

这是另一个:

min(random.randint(a, b), random.randint(a, b), random.randint(a, b))

随着 random.randint(a, b) 越来越多,它们的最小值趋向于越来越集中在范围的下端.

With more and more random.randint(a, b), their minimum tends to be more and more concentrated towards the lower end of the range.

用户pjs"写了以下评论:

The user "pjs" wrote the following comment:

这两者都可以推广到相同的形式,k阶统计量的最小值,可以使用单个随机数生成,然后缩放到正确的范围:int(math.floor(a + (b - a + 1) * (1.0 - random.random()**(1.0/k)))) .当 k == 2 这有一个三角形分布,并且对于更高的 k 值,它变得越来越重于 a.如果您想玩具有方差减少"的游戏,则基于单个随机数也使此方法适用于常见随机数或对立随机数.Monte Carlo sims 中的策略.

Both of those can be generalized to the same form, the minimum of k order statistics, which can be generated using a single random number and then scaled to the correct range:int(math.floor(a + (b - a + 1) * (1.0 - random.random()**(1.0 / k)))). When k == 2 this has a triangle distribution, and for higher values of k it becomes more and more heavily weighted towards a. Basing it on a single random number also makes this method amenable to common random numbers or antithetic random numbers if you want to play games with "variance reduction" strategies in Monte Carlo sims.

但是,这个公式存在问题.

However, there are issues with this formula.

  • 一方面,存在准确性问题:表达式 random.random()**(1.0/k) 在 1 附近是病态的,对于大 k 接近 1code>,因此在从 1/2 到 1 比从 0 到 1/2 更粗糙的浮点算术中,可能存在精度问题".(Devroye,1986,非均匀随机变量生成,第 675 页).
  • 其次,调用浮点数只是为了最终输出随机整数是相当不雅的——毕竟,计算机通过转换整数来生成随机浮点变量,而不是相反.
  • 最后,我们通常不应该关心随机变量生成方法之间的效率(性能),除非我们在应用程序中使用它们并发现运行时间不可接受.这是称为过早优化"的一般编程问题.虽然我在这个答案中的方法可能使用许多随机数,但它很方便,特别是因为我们可以将其重写为:min(random.randint(a, b) for i in k) for some integer k 大于 0.
  • For one, there are issues of accuracy: the expression random.random()**(1.0 / k) is ill-conditioned near 1 and approaches 1 for large k, so that in floating-point arithmetic which is coarser from 1/2 to 1 than from 0 to 1/2, "there could be an accuracy problem" (Devroye, 1986, Non-Uniform Random Variate Generation, page 675).
  • Second, it's rather inelegant to invoke floating-point numbers just to output random integers in the end — after all, computers generate random floating-point variates by transforming integers, not the other way around.
  • And finally, we should generally not be concerned about efficiency (performance) between random variate generation methods, unless we have used them in an application and found the running time to be unacceptable. This is a general programming issue known as "premature optimization". Although my approach in this answer may use many random numbers, it's convenient, especially since we can rewrite it as:min(random.randint(a, b) for i in k) for some integer k greater than 0.

这篇关于为较低的数字生成具有较高概率的随机自然数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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