我如何编写包含小数的偏置数字生成器 - Lua

How Can I Program A Biased Number Generator That Includes Decimals - Lua

我怎样才能做到这一点,以便我可以为 1.0-10.0 中的每个数字分配一个值,该值确定它被选中的机会,如果可能的话不使用大量的 if 语句?

如何运作:

1.0 = 1%, 1.1 = 1.1%, 1.2 = 1.5%, 1.3 = 1.7%, until 1.9 which would make 1.0-2.0 have 10% chance of being picked

1.0-2.0 would be have the second highest rate, 2.0-4.0 would have the highest rate, 4.0 5.0 would have the third highest chance, 5.0-6.0 would have the fourth, 6.0-7.0 would have the fifth, 7.0-8.0 would have the sixth, and so on until 10.0

用足够多的值填充 table,根据您的概率填充这些值,然后从 table 中随机抽取。这应该是最简单的方法了。

例如,制作一个包含 1000 个元素的 table,前十个元素的值为 1.0(因此,如果随机抽取,则概率为 1%),然后 11 个元素为 1.1,15 个元素为1.2 等等。