0

I was playing around with the lua interpretor and made this little program that generates two numbers and compares them. The program runs until the numbers match. The first number is randomly generated using math.random(), and is set to 1, and 100000. The second value that is generated to compare is between 1 and 100. It also keeps track of how many times the program loops. The program works as intended, but something strange happens when I run it.

The values that come up are always either 1, 31, 62, or 92. I've run the program many times, but it keeps generating these numbers. I have some understanding of how random numbers are generated, but this just seems weird. I'll paste the code in below. If someone can explain what's going on here, I would appreciate it greatly. Thanks!

counter=0;
a=0;
b=1;
while(a~=b)do
    a=math.random(1,1000000);
    b=math.random(1,100);
    counter=counter+1;
    if(a==b)then
         print(a..", "..b..", and it took "..counter.." times")
    end
end
  • What version of Lua is this? The latest version automatically seeds the RNG so this doesn't happen. – luther Dec 27 '21 at 17:17
  • It says lua 5.1 I could've swore I got this directly from lua.org. Google says the latest version is 5.4. Thanks for letting me know. Is there somewhere I can go to read more about what causes this though? – RogueEugor Dec 27 '21 at 17:59
  • Run it in the [online demo](https://www.lua.org/demo.html). I get different numbers every time. – lhf Dec 27 '21 at 18:42
  • On Lua 5.1 you should start your program with `math.randomseed(os.time())` – Egor Skriptunoff Dec 29 '21 at 05:27

1 Answers1

0

The behaviour of the pseudo-random number generator changed in Lua 5.4. The 5.4 Reference Manual states, under §8.2 – Incompatibilities in the Libraries, that

The pseudo-random number generator used by the function math.random now starts with a somewhat random seed. Moreover, it uses a different algorithm.

If you are curious, that new algorithm is xoshiro256**.

What this means is that programs running in Lua environments prior to 5.4 must explicitly call math.randomseed to seed the pRNG. You want to do this only once in your program.

Without seeding the pRNG, the sequence of numbers produced will be the same each time you run your program. This is because, prior to 5.4, Lua's math.random is implemented using C rand or POSIX random, both of which default to a seed of 1 if not explicitly seeded.

The classic way to seed a pRNG is to use the current time (os.time). This is a simple approach, but has the fault that running this program twice in the same second will have the same result.

local counter = 0
local a = 0
local b = 1

math.randomseed(os.time())

while a ~= b do
    a = math.random(1, 1000000)
    b = math.random(1, 100)
    counter = counter + 1

    if a == b then
        print(a .. ", " .. b .. ", and it took " .. counter .. " times")
    end
end

Documentation links, for comparison:

5.3: math.random math.randomseed
5.4: math.random math.randomseed

Oka
  • 23,367
  • 6
  • 42
  • 53