I am supposed to verify the capability of the random number generator of our implementation of Lua.
This is what I came up with...
for i = 1,10000 do -- I hope that's ten thousand
X = math.random(0,9)
if X == 0 then This_Many_0 = This_Many_0 + 1
elseif X == 1 then This_Many_1 = This_Many_1 + 1
elseif X == 2 then This_Many_2 = This_Many_2 + 1
elseif X == 3 then This_Many_3 = This_Many_3 + 1
elseif X == 4 then This_Many_4 = This_Many_4 + 1
elseif X == 5 then This_Many_5 = This_Many_5 + 1
elseif X == 6 then This_Many_6 = This_Many_6 + 1
elseif X == 7 then This_Many_7 = This_Many_7 + 1
elseif X == 8 then This_Many_8 = This_Many_8 + 1
elseif X == 9 then This_Many_9 = This_Many_9 + 1
else Bogus_Alert = True -- Better not happen
end -- End the big if/elseif block
The_Interim_Sum = The_Interim_Sum + X -- Keep a running sum
end -- This is the i loop
I then printed the results, here they are...
Running
Number of times we got 0 ... 1019
Number of times we got 1 ... 979
Number of times we got 2 ... 954
Number of times we got 3 ... 1006
Number of times we got 4 ... 995
Number of times we got 5 ... 999
Number of times we got 6 ... 989
Number of times we got 7 ... 1000
Number of times we got 8 ... 1042
Number of times we got 9 ... 1017
The sum of the ten thousand random numbers was 45303
The average of those numbers was 4.5303
End
These follow my expectations; i.e., the average is very close to 4.5, and the distribution of each individual number is close to one thousand.
More to the point, with numbers like these, I say that yes indeed, that generator is doing a very good job of really making ten thousand random numbers that really are random.
Here's the point of confusion: my boss says that if the machine is truly doing its job, and truly giving us random numbers, then the distribution of each number should be even; i.e., in ten thousand iterations, I should get precisely one thousand of each digit.
I tried adding math.randomseed(os.time())
before the loop which was using X = math.random(0,9)
based on some other conversations here on StackOverflow. The results were further off, not closer.
I took out the math.randomseed(os.time())
line, ran the test again, and got an average of 4.5007 (which is the best I've seen yet).
So, what have I discovered ? Anything ? Nothing ? A random number generator is really a *pseudo*random number generator ? Everybody already knows that.
Have I demonstrated that this pseudo random number generator is about as random as we can expect ?
Do I have reason to fear using these values ? I'm going to take pot shots at a routine and give him deliberately bad numbers. The full range is too much for testing each and every case. I'm guessing that properly distributed, 16K potshots will give me the confidence that the chip is handling the (purposely bad) values properly.
(Note for the reader, I'm doing V&V on a multiprocessor system, and my Lua scripts are faking the behavior of one of the parts of the system which we don't have yet.)
Is there a way that I can get Lua to generate exactly one thousand of each number in this case with the math.random()
function ?
If so, would that truly be random ?