Seen while digging the random “problem” on mac osx:
"Lua's random uses C's rand and srand functions.
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."
I ran into the “problem” too. The simulator on lua.org executes the code you type on a linux machine, hence bypassing our osx platform behavior.
"Lua on mac osx converts the integer returned by rand() into
a real number, effectively preserving only the high bits in the result.
When you call math.random(1,100) from Lua on mac osx,
the low-bit difference vanishes and you see the same integer result."
Here is what I am using to bypass the “same number always” oddity (works ok but not the best):
local \_seed = os.time()
local function getRandom(f,c)
\_seed = \_seed + 1
math.randomseed(\_seed)
math.random()
local a = math.random(f,c)
\_seed = \_seed + a
return (a)
end
--get a random integer between 1 and 10
print (getRandom(1,10))
Maybe Ansca would work on that ? Quite annoying, indeed
[import]uid: 6050 topic_id: 924 reply_id: 2481[/import]