次の lua コード スニペットを検討してください。
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
Linux マシンでは、予想どおり、結果は乱数になります。しかし、少なくとも Mac OS X では、シードを変更した後の最初の乱数は常に同じです。
これは、Lua が乱数を生成するために C の rand() 関数に依存しているという事実に関連していると思いますが、誰か説明がありますか?
編集: これは、Linux マシンでの上記のコードの出力の抜粋です (つまり、出力は期待どおりです)。
$ lua test.lua
Seeding with 1232472273
69
30
83
59
84
Seeding with 1232472274
5
21
63
91
27
[...]
OS X マシンでは、「Seeding with ...」の後の最初の数字は常に 66 でした。