Give the 'random' builtin it's own seed state, to keep other users of random data from creating a generating a non-deterministic sequence of numbers even if the user manually seeds

darcs-hash:20060615103556-ac50b-7865016b4561c7c69afb1ba77f85adeda1da691e.gz
This commit is contained in:
axel 2006-06-15 20:35:56 +10:00
parent e9e0643817
commit f673b06dd2

View File

@ -1286,6 +1286,8 @@ static int builtin_function( wchar_t **argv )
static int builtin_random( wchar_t **argv )
{
static int seeded=0;
static struct drand48_data seed_buffer;
int argc = builtin_count_args( argv );
woptind=0;
@ -1346,18 +1348,22 @@ static int builtin_random( wchar_t **argv )
case 0:
{
long res;
if( !seeded )
{
seeded=1;
srand( time( 0 ) );
srand48_r(time(0), &seed_buffer);
}
sb_printf( sb_out, L"%d\n", rand()%32767 );
lrand48_r( &seed_buffer, &res );
sb_printf( sb_out, L"%d\n", res%32767 );
break;
}
case 1:
{
int foo;
long foo;
wchar_t *end=0;
errno=0;
@ -1372,7 +1378,7 @@ static int builtin_random( wchar_t **argv )
return 1;
}
seeded=1;
srand( foo );
srand48_r( foo, &seed_buffer);
break;
}