lua-users home
lua-l archive

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


> On Apr 13, 2018, at 11:01 AM, Dirk Laurie <dirk.laurie@gmail.com> wrote:
> 
> 2018-04-13 16:24 GMT+02:00 Albert Chan <albertmcchan@yahoo.com>:
>> When to use lua upvalue mechanism instead of simpler C static variable ?
>> 
>> Example, lua 5.4 work-1 math.random :
>> 
>> It uses upvalue to store the 128-bits random state.
>> Why not use static uint64_t state[2] ?
> 
> Otherwise the code is not reentrant.
> 
> Suppose two processes on Unix run Lua. Only one copy of the Lua shared
> library gets loaded.

Is this one copy shared library thing only for UNIX ?

> If a static variable is used, it gets updated when either process asks
> for a random number. This violates the contract that the same seed
> will buy you the same pseudo-random sequence. If an upvalue is used,
> each process has its own independent pseudo-random sequence.
> 

Prior to lua 5.4, lua math.random() use  C rand().
rand() does not know about lua upvalues.

Does that mean the "old" lua math.random may obtain different 
random output with the same seed ?