[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: RE: Unexpected randomseed results (was [ANN] uuid: pure Lua uuid generator)
- From: Thijs Schreijer <thijs@...>
- Date: Sun, 12 May 2013 20:53:41 +0000
> -----Original Message-----
> From: lua-l-bounces@lists.lua.org [mailto:lua-l-bounces@lists.lua.org] On
> Behalf Of Sean Conner
> Sent: zaterdag 11 mei 2013 10:23
> To: Lua mailing list
> Subject: Re: Unexpected randomseed results (was [ANN] uuid: pure Lua uuid
> generator)
>
> It was thus said that the Great Sean Conner once stated:
> >
> > In thinking about it, Lua should do the following:
> >
> > srand(luaL_checkint(L,1) % INT_MAX);
>
> Ooops, that's silly. What I meant was:
>
> srand((unsigned int)fmod(luaL_checknumber(L,1),UINT_MAX));
>
> for both Lua 5.1 and 5.2.
>
> Or perhaps:
>
> double ds = luaL_checknumber(L,1);
> unsigned int s;
>
> if (ds < 1.0)
> s = (unsigned int)(ds * (double)UINT_MAX);
> else if (ds > UINT_MAX)
> s = (unsigned int)fmod(ds,UINT_MAX);
> else
> s = (unsigned int)ds;
>
> srand(ds);
>
> to force it into a range of 0 .. UINT_MAX.
>
Nice, but even then, I think a word of warning in the manual would be welcome. Or as mentioned before; simply return the seed as set, so the script can check whether everything is ok.
Thijs
> -spc (Or just keep it as is and be a bit more careful about seeding)
>