summaryrefslogtreecommitdiff
path: root/random.c
diff options
context:
space:
mode:
authormatz <matz@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2000-01-08 05:00:25 +0000
committermatz <matz@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2000-01-08 05:00:25 +0000
commita69b9bce318b3d4717b92a8747ecdb4e71585c5d (patch)
treef12f100a2b4900f3533586fa342cad65713f7855 /random.c
parenta3da8465b4406e01b9b597e0af81dad3f65c5528 (diff)
2000-01-08
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@601 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'random.c')
-rw-r--r--random.c48
1 files changed, 33 insertions, 15 deletions
diff --git a/random.c b/random.c
index 62d300a..e8c9d1c 100644
--- a/random.c
+++ b/random.c
@@ -81,6 +81,30 @@ static int first = 1;
static char state[256];
#endif
+static int
+rand_init(seed)
+ long seed;
+{
+ int old;
+ static unsigned int saved_seed;
+
+#ifdef HAVE_RANDOM
+ if (first == 1) {
+ initstate(1, state, sizeof state);
+ first = 0;
+ }
+ else {
+ setstate(state);
+ }
+#endif
+
+ SRANDOM(seed);
+ old = saved_seed;
+ saved_seed = seed;
+
+ return old;
+}
+
static VALUE
rb_f_srand(argc, argv, obj)
int argc;
@@ -89,7 +113,6 @@ rb_f_srand(argc, argv, obj)
{
VALUE a;
unsigned int seed, old;
- static unsigned int saved_seed;
if (rb_scan_args(argc, argv, "01", &a) == 0) {
static int n = 0;
@@ -101,20 +124,7 @@ rb_f_srand(argc, argv, obj)
else {
seed = NUM2UINT(a);
}
-
-#ifdef HAVE_RANDOM
- if (first == 1) {
- initstate(1, state, sizeof state);
- first = 0;
- }
- else {
- setstate(state);
- }
-#endif
-
- SRANDOM(seed);
- old = saved_seed;
- saved_seed = seed;
+ old = rand_init(seed);
return rb_uint2inum(old);
}
@@ -125,6 +135,14 @@ rb_f_rand(obj, vmax)
{
long val, max;
+ static initialized = 0;
+
+ if (first) {
+ struct timeval tv;
+
+ gettimeofday(&tv, 0);
+ rand_init(tv.tv_sec ^ tv.tv_usec ^ getpid());
+ }
switch (TYPE(vmax)) {
case T_FLOAT:
if (RFLOAT(vmax)->value <= LONG_MAX && RFLOAT(vmax)->value >= LONG_MIN)