你的示例代码有一个硬错误:你写的150/208和190/209.这是一个整数除法,两者都是零.您应该编写:150.0/208并190.0/209指示编译器将它们分为double而不是整数.
编辑:
假设系统的RNG是平的并且您的表格如下:
[item] [amount]
0 3 000 000
25 1 500 000
50 2 000 000
75 300 000
100 10 000
150 10 000 (no typo)
sum = 6820000
Run Code Online (Sandbox Code Playgroud)
那么你的随机数发生器看起来像:
int randomItemNumber = Random.Next(6820000); // 0..6819999
if(randomItemNumber < 3000000)
Console.WriteLine("Aah, you've won the Item type #0\n");
else if(randomItemNumber < 3000000+1500000)
Console.WriteLine("Aah, you've won the Item type #1\n");
else if(randomItemNumber < 3000000+1500000+2000000)
Console.WriteLine("Aah, you've won the Item type #2\n");
else if(randomItemNumber < 3000000+1500000+2000000+300000)
Console.WriteLine("Aah, you've won the Item type #3\n");
else if(randomItemNumber < 3000000+1500000+2000000+300000+10000)
Console.WriteLine("Aah, you've won the Item type #4\n");
else if(randomItemNumber < 3000000+1500000+2000000+300000+10000+10000)
Console.WriteLine("Aah, you've won the Item type #5\n");
else
Console.WriteLine("Oops, somehow you won nothing, the code is broken!\n");
Run Code Online (Sandbox Code Playgroud)
这个想法是你把所有物品一个接一个地放在一个looong线上,但你把它们放在他们的小组中.所以,一开始有第一种类型的三百万,然后是第二种类型的数百万,依此类推.该行共有6820000项.现在,您随机选择1到6820000(或从0到6819999)的数字,并将其用作LINE中元素的NUMBER.
由于项目存在于具有正确统计分布的行中,如果随机化1-6820000是FLAT,那么得到的"抽奖"将具有您想要的完全分布.
唯一需要解释的技巧是如何猜测挑选的物品.这就是我们将这些项目分组的原因.3000000项的第一部分是第一部分,所以如果数字小于3000000那么我们就打第一类.如果超过那个,但低于下一个1500000(低于450000)那么第二个类型被击中..依此类推.
| 归档时间: |
|
| 查看次数: |
2515 次 |
| 最近记录: |