diff options
| author | akr <akr@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2005-01-03 02:41:03 +0000 |
|---|---|---|
| committer | akr <akr@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2005-01-03 02:41:03 +0000 |
| commit | 35100a5a039f9c3a7d5de12c8a19115ec7f9a0f2 (patch) | |
| tree | 37287fe87c566240b2f6baf6a0d87883718bfba5 /random.c | |
| parent | fc1a6902d2fe8f5249f4fa3dd4c3e72bb0be7517 (diff) | |
| download | ruby-35100a5a039f9c3a7d5de12c8a19115ec7f9a0f2.tar.gz ruby-35100a5a039f9c3a7d5de12c8a19115ec7f9a0f2.tar.xz ruby-35100a5a039f9c3a7d5de12c8a19115ec7f9a0f2.zip | |
* random.c (random_seed): use /dev/urandom if available.
[ruby-dev:25392]
git-svn-id: http://svn.ruby-lang.org/repos/ruby/trunk@7713 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'random.c')
| -rw-r--r-- | random.c | 20 |
1 files changed, 19 insertions, 1 deletions
@@ -144,6 +144,11 @@ genrand_real() #include <unistd.h> #endif #include <time.h> +#include <sys/types.h> +#include <sys/stat.h> +#ifdef HAVE_FCNTL_H +#include <fcntl.h> +#endif static int first = 1; @@ -167,9 +172,22 @@ random_seed() { static int n = 0; struct timeval tv; + unsigned long result; + int fd; + unsigned long buf; gettimeofday(&tv, 0); - return tv.tv_sec ^ tv.tv_usec ^ getpid() ^ n++; + result = tv.tv_sec ^ tv.tv_usec ^ getpid() ^ n++; + + result += (unsigned long)&result; + + if ((fd = open("/dev/urandom", O_RDONLY)) >= 0) { + read(fd, &buf, sizeof(buf)); + close(fd); + result ^= buf; + } + + return result; } /* |
