mercredi 11 septembre 2019

Lua math.random() returning 0 too often

I am playing with distributions and histograms in Lua for which i need to generate random numbers between 0 and 1. Since I'm generating tens of thousands of these random numbers and calculating the log of each one of them, a problem that never happened to me when using the math.random() function before appeared: it is generating unexpectedly high amounts of zeros. Since it yields a number with 14 decimals, I was expecting to get roughly one 0 every 10^14 times the function was called, but I find anything up to 5 or 6 of them when calling math.random() 100,000 times. Here's a simple test I made to confirm this. The main problem is that, since I must calculate the log of these random values, math.log(0) returns -inf, which messes up with most of my variables like average and standard deviation.

It's not a serious project and I can easily work around this problem in many ways, even calculating a new random number every time I get a 0, but I would like to know what's wrong here. I tried the same thing in Python and did not have this issue. I also found out that math.random() would not return any number less than 0.00001 (except 0), which makes me think this is a rounding problem.

So what's wrong and how can I get a more uniformly distributed number generator for the [0,1) interval? Thanks for any help!




Aucun commentaire:

Enregistrer un commentaire