tags:

views:

877

answers:

5

Consider the following lua code snippet :

local time = os.time()
for _= 1, 10 do
    time = time + 1
    print('Seeding with ' .. time)
    math.randomseed(time)
    for i = 1, 5 do
     print('\t' .. math.random(100))
    end
end

On a Linux machine, the result is, as expected, random numbers. But it seems that at least on Mac OS X, the first random number after changing the seed is always the same !

I guess this is related to the fact that Lua relies on the C rand() function for generating random numbers, but does anybody have an explanation ?

EDIT: here is an extract of the output of the above code on a linux machine (ie the output is as expected) :

$ lua test.lua
Seeding with 1232472273
    69
    30
    83
    59
    84
Seeding with 1232472274
    5
    21
    63
    91
    27
[...]

On an OS X machine, the first number after "Seeding with ..." was always 66.

A: 

If you use the same seed, you will get the same string of numbers from the C rand() function, but you should get a different string of numbers each time since you appear to be using the current time as the seed.

Edit: I suppose I should elaborate on my answer. If you are not getting a random string of numbers when seeding with os.time(), you may not be getting what you expect from that function call. What are the values you are getting back from os.time()?

Edit #2: Also, what is the output from that block of code?

Matthew Brubaker
I'm well aware of the fact that using the same seed gives the same sequence of pseudo-random numbers. My question is about the fact that with different seeds, the first generated number seems to be the same on some platforms.
Wookai
+1  A: 

It's generally a bad idea to call srand multiple times with seeds that are numerically close (and especially bad to do so with time values). In many cases, the variance of the first random number is similar to the variance of the seeds. When dealing with a scripting language that has to convert number representations, it can be even more so.

Does the same thing occur if you change your seed value by a larger amount?

Gerald
+3  A: 

Lua's random uses C's rand and srand functions (see here).

The C90 standard defines the precise implementation of rand and srand, and this algorithm isn't the best. It especially lacks randomness in the lower bits.

Some platforms like Linux changed the implementation of rand to an intentionally non-conforming, and better, implementation (e.g. random(3)).

OS/X remains true to the standard rand implementation, and Lua inherits it.

The real fix should be for Lua to use random(3), drand48(3), or any better random implementation by default.

orip
That still doesn't really explain why he's always getting 66 as the first number back, but it is good information.
Matthew Brubaker
A: 

As others noted, Lua intentionally uses C90 random generator for portability sake -- and C90 RNG is not very good.

If you need good random numbers, use some Lua module to get it. For example, here is Mersenne Twister RNG binding by one of Lua authors.

Alexander Gladysh