Search code examples
lualua-table

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


How can I make it so I can assign each number from 1.0-10.0 a value that determines what chance it has on being picked that doesn't use a mass amount of if statements if possible?

How it would work:

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


Solution

  • Populate a table with large enough number of values that is filled with values according to your probabilities and draw randomly from that table. That should be the easiest way.

    For example, make a table of 1000 elements and the first ten will have the value 1.0 (so if it's drawn randomly, you get 1% probability), then 11 elements with 1.1, 15 elements with 1.2 and so on.