aboutsummaryrefslogtreecommitdiffhomepage
path: root/builtin.c
diff options
context:
space:
mode:
authorGravatar axel <axel@liljencrantz.se>2006-06-15 20:35:56 +1000
committerGravatar axel <axel@liljencrantz.se>2006-06-15 20:35:56 +1000
commitf673b06dd239f73ca847168f9a48053751329b07 (patch)
tree48534f99058e9ce805c19e667bc5946900dd2375 /builtin.c
parente9e0643817806cad56f05a9f68b79b541305054e (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.c14
1 files changed, 10 insertions, 4 deletions
diff --git a/builtin.c b/builtin.c
index e1997b20..bd2bc501 100644
--- a/builtin.c
+++ b/builtin.c
@@ -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;
}