lua-users home
lua-l archive

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


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)."

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.)

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?

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


C:\>lua5.1 -v randomtest_2.lua
Lua 5.1.1  Copyright (C) 1994-2006 Lua.org, PUC-Rio
Seeding with 1174327833
66
84
24
35
30
Seeding with 1174327834
66
17
78
9
62
Seeding with 1174327835
66
50
33
82
93
Seeding with 1174327836
66
83
87
56
25
Seeding with 1174327837
66
15
42
29
56
Seeding with 1174327838
66
48
96
3
88
Seeding with 1174327839
66
81
51
76
19
Seeding with 1174327840
66
14
5
49
51
Seeding with 1174327841
66
47
60
23
82
Seeding with 1174327842
66
79
14
96
14




[1] http://lua-users.org/wiki/MathLibraryTutorial