diff options
author | 2006-06-15 20:35:56 +1000 | |
---|---|---|
committer | 2006-06-15 20:35:56 +1000 | |
commit | f673b06dd239f73ca847168f9a48053751329b07 (patch) | |
tree | 48534f99058e9ce805c19e667bc5946900dd2375 /builtin.c | |
parent | e9e0643817806cad56f05a9f68b79b541305054e (diff) |
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
Diffstat (limited to 'builtin.c')
-rw-r--r-- | builtin.c | 14 |
1 files changed, 10 insertions, 4 deletions
@@ -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; } |