lua-users home
lua-l archive

Re: Unexpected randomseed results (was [ANN] uuid: pure Lua uuid generator)

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


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.
 -spc (Or just keep it as is and be a bit more careful about seeding)

AltStyle によって変換されたページ (->オリジナル) /