问题陈述:用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/