lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


On 17/03/2010, at 8:36 AM, Geoff Leyland wrote:

> On 17/03/2010, at 7:24 AM, Christopher Eykamp wrote:
> 
>> 
>> On 3/16/2010 11:18 AM, Geoff Leyland wrote:
>>> 
>>> On windows I got the same results as Christopher.
>>> 
>>> Lua just uses the system's rand, and what you're learning is that rand isn't a great random number generator, and that Windows' implementation is not the best.  lhf's lrandom is surely better: http://www.tecgraf.puc-rio.br/~lhf/ftp/lua/
>>> 
>> Even a poorly designed random number generator should generate different streams from different seeds.  Is it possible that lua is not passing the seed off to the system's random function properly in some implementations?
> 
> Sorry, you might be right.  There could be an interaction between the assembler Lua uses to convert a double to an integer on Windows and srand.  I'm way out of my depth here, but I get this output:

Excuse me for replying to myself.  There was a mistake in the code I sent in printing the random seeds.  The last line in srand_normal and srand_asm should be:

 printf("%u\t", s);

In which case the output from srand_asm is:

ASM
2147483648      38=12   7719=2356       21238=6482      2437=744
2147483648      38=12   7719=2356       21238=6482      2437=744
2147483648      38=12   7719=2356       21238=6482      2437=744
2147483648      38=12   7719=2356       21238=6482      2437=744
2147483648      38=12   7719=2356       21238=6482      2437=744
2147483648      38=12   7719=2356       21238=6482      2437=744

Note that all the seeds are the same!

I think the problem is that on some platforms the arguments to math.random get truncated to an int, even though srand takes an unsigned int as an argument.  All your seeds are larger than an int.

Cheers,
Geoff