lua-users home
lua-l archive

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


On Mon, Mar 19, 2007 at 12:20:39PM -0600, Gavin Kistner wrote:
> The Lua Wiki Math Library Tutorial page[1] says:
> "But beware! The first random number you get is not really 'randomized'
> (at least in Windows 2K and OS X)."

You should not call math.randomseed() multiple times with similar (like
prev. + 1) values. This will lead to similar values returned by successive
rand() calls.

You should only call math.randomseed() ONCE!
 
> I observe this same behavior locally. The first number returned by
> math.random() after a call to math.randomseed() is always the same. (See
> output below.)

This is because of similar seeds. You don't really get the same value but
similar values. Try to set the interval to (math.random(0, 32767)) and you
will see the difference.

> Is this acceptable? Is it by design? Wouldn't it be better if (lacking
> any other possible fix) math.randomseed() called math.random() once at
> the end to throw away the static value?

Only call math.randomseed() once and use more random input values like
microseconds and process id.

> I'm not interested in cryptographically-secure randomness, but I'd like
> my random to be...well...random! :)
> 
> 
> C:\>type randomtest_2.lua
> local theSeed = os.time()
> for _=1,10 do
>   theSeed = theSeed + 1
>   print( "Seeding with "..theSeed )
>   math.randomseed( theSeed )
>   for i=1,5 do
>     print( math.random( 1, 100 ) )
>   end
> end
Should be:

local theSeed = os.time()  
print( "Seeding with "..theSeed )
math.randomseed( theSeed )
for x=1,10 do
  for i=1,5 do
  print(math.random(1,100))
  end
end


Jürgen