#Generate numbers without float imprecision

1 messages · Page 1 of 1 (latest)

hardy pumice
#

I need to generate numbers up to 4 decimal places between 0 and 1 (inclusive), with the rarity for each value distributed like a bell curve centered at 0.5 (0.5 is the most common, and it gets progressively more rare the closer you get to 0 or 1). I have tried a lot of methods to produce this, but they usually result with 0 and 1 being more common than their previous values (0.0001 and 0.9999) due to floating point precision/rounding errors. The only clean solution I have come up with is to assign a weight to each value, and then pick a value based on its weight. This seems like overkill though so I was wondering if anyone has a cleaner solution.

agile mica
#

use math.round

#

local result = 2.5 + 2.5 -- Might be 5.000000000001 internally
local rounded = math.round(result)
print(rounded) -- Will output exactly 5

hardy pumice