Why is the first random number always the same on some platforms in lua?

前端 未结 5 1081
眼角桃花
眼角桃花 2021-01-18 15:50

Consider the following lua code snippet :

local time = os.time()
for _= 1, 10 do
    time = time + 1
    print(\'Seeding with \' .. time)
    math.randomseed         


        
5条回答
  •  孤城傲影
    2021-01-18 16:35

    Lua's random used to use C's rand(3) and srand(3) functions (see here). UPDATE: newer Lua versions use random(3) where available.

    Both the C90 standard and POSIX suggest an cross-platform implementation of rand and srand that isn't the best. It especially lacks randomness in the lower bits.

    Some platforms like Linux moved off of the standard recommendation to a better implementation (e.g. random(3)).

    OS/X remains true to the classic rand implementation, and Lua inherits it.

提交回复
热议问题