2
votes

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!

3
I tried it online and I don't see this behavior ... What OS are you running btw? - Saswat Padhi
Windows 7. Lua 5.3.4 - Thacio
Yes, I have the same results on Windows (frequency of 0 is more than 2^-16). This means that Windows PRNG is bad. Try to use Lua 5.4.0-alpha. Lua 5.4 uses its own PRNG instead of the one provided by OS. - Egor Skriptunoff
@EgorSkriptunoff Didn't LuaJIT also use its own PRNG? Or am I just remembering things wrong? - DarkWiiPlayer
@DarkWiiPlayer - Yes, LuaJIT does use its own PRNG. But OP is using Lua 5.3, so LuaJIT might be not a suitable replacement. - Egor Skriptunoff

3 Answers

0
votes

you can try to drop 0 values and move the range like this:

local function rnd()
   local base = 100000
   local x = (math.random(0,base+1)-1)/base
   if x<0 then return rnd() end
   return x
end

local function test(n)
  local k=0
  local x
  math.randomseed(os.time()) 
  for i=1,n do
    x = rnd()
    if x==0 then k=k+1 end
  end
  print("k=",k)
end

test(100000)

if we reduce the base value for example to 10, then you can see an acceptable amount of limit values 0 and 1

0
votes

I wasn't able to replicate your results at all!

Here's my test code:

math.randomseed(os.time())

function test(n)
  local k = 0
  for i=1, n do
    if math.random() == 0 then
      k = k + 1
    end
  end
  return k / n
end

for i=1,6 do
  print(string.format("%8.4f %%", test(1e7) * 100))
end

And the results are:

PUC Lua 5.3.5

  0.0000 %
  0.0000 %
  0.0000 %
  0.0000 %
  0.0000 %
  0.0000 %

LuaJIT 2.1.0-Beta3

  0.0000 %
  0.0000 %
  0.0000 %
  0.0000 %
  0.0000 %
  0.0000 %

As a simple workaround, I suggest doing this

local function random()
  local result = math.random() 
  return result>0 and result or random()
end

Alternatively, just return result>0 and result or 1, since 1 will never be returned by math.random() anyway, so if you don't mind having a few 1s in there, that's another option.

0
votes

So the problem was probably due to Windows PRNG, as Egor Skriptunoff pointed out. I'm still intrigued by the fact that no numbers between 0 and 0.00001 are generated though. Anyways I'm using LuaJIT now and the problem is solved (without workarounds)! Thanks, everyone!