diff options
author | matz <matz@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 1998-04-27 10:04:11 +0000 |
---|---|---|
committer | matz <matz@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 1998-04-27 10:04:11 +0000 |
commit | 9e48333190cb95ecd4d8a49eed103518457e8ace (patch) | |
tree | 0a28d6aa4707629c5d106e979c316c5b65b72e60 /random.c | |
parent | 043c693d6a7521604ced80a0619d38b4f187f1c9 (diff) |
tcltklib/gtk
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/branches/v1_1r@193 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'random.c')
-rw-r--r-- | random.c | 23 |
1 files changed, 14 insertions, 9 deletions
@@ -24,8 +24,12 @@ struct timeval { #endif #endif /* NT */ -static int first = 1; +#ifdef HAVE_STDLIB_H +# include <stdlib.h> +#endif + #ifdef HAVE_RANDOM +static int first = 1; static char state[256]; #endif @@ -43,7 +47,7 @@ f_srand(argc, argv, obj) struct timeval tv; gettimeofday(&tv, 0); - seed = tv.tv_usec; + seed = tv.tv_sec ^ tv.tv_usec; } else { seed = NUM2INT(seed); @@ -78,16 +82,12 @@ f_rand(obj, vmax) { int val, max; - if (first == 1) { - f_srand(0, 0, 0); - } - switch (TYPE(vmax)) { case T_BIGNUM: return big_rand(vmax); case T_FLOAT: - if (RFLOAT(vmax)->value > LONG_MAX || RFLOAT(vmax)->value < LONG_MIN) + if (RFLOAT(vmax)->value > INT_MAX || RFLOAT(vmax)->value < INT_MIN) return big_rand(dbl2big(RFLOAT(vmax)->value)); break; } @@ -96,9 +96,14 @@ f_rand(obj, vmax) if (max == 0) ArgError("rand(0)"); #ifdef HAVE_RANDOM - val = random() % max; + val = random(); +#else + val = rand(); +#endif +#ifdef RAND_MAX + val = val * (double)max / (double)RAND_MAX; #else - val = rand() % max; + val = (val>>8) % max; #endif if (val < 0) val = -val; |