java SecureRandom nextInt() 与 nextGaussian()

标签 java math random

问题陈述:用1%到100%之间X%折扣的抽奖券奖励客户

假设 slab 是预定义的(都是理论上的)

1% discount : 90% customers

10% discount : 5% customers

20% discount : 3% customers

100% discount : 2% customers

解决方案一:

对于每 100 个客户,我将使用良好的洗牌算法预先填充一个大小为 100 的随机数组

90 1s ( 1 = 1% discount)

5 10s ( 10 = 10% discount)

3 20s ( 20% discount )

2 100s ( 100% discount)

每当有顾客来时,我会抽取random.nextInt(100)并获得折扣百分比

解决方案 2:

使用 SecureRandom 的 nextGaussian(),因为它是一种分布式随机算法。

如果我们要严格按照百分比,哪个更准确?

请参阅 Random 中的示例 1 和示例 3

提前致谢。

最佳答案

Which one is more accurate if we want to follow the % strictly? e.g give 100% discount to 1 customer from every 100 customers.

都不是,因为这不再是随机的。如果您想要在给定时间间隔内进行精确分布,则必须使用不同的策略。用您想要的值分布填充Collection,将其打乱,然后将值分配给您的客户。

关于java SecureRandom nextInt() 与 nextGaussian(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32233896/

相关文章:

java - wsimport 在将 wsdl 解析为 java 时抛出与包相关的错误

javascript - 将时间增量和速度纳入速度/摩擦运动

java - Java的SecureRandom循环

java - java swing 中的 jbutton(浏览 pc 文件夹)

java - 前台服务和 Android Wear 通知

java - 如何在JavaFX TextArea中显示日志?

opencv - 如何在 Mat 中查找特定区域中的非零像素

algorithm - 如何生成凸多边形

php rand() 函数显示负值

c++ - C++ 中的伯努利随机变量