Mercurial > hg > mlmmj
changeset 169:6d3b77645857
Bye bye FILE*
author | mmj |
---|---|
date | Fri, 04 Jun 2004 02:57:14 +1000 |
parents | 88772c4e7bd4 |
children | a9115cfd8564 |
files | src/random-int.c |
diffstat | 1 files changed, 28 insertions(+), 11 deletions(-) [+] |
line wrap: on
line diff
--- a/src/random-int.c Fri Jun 04 02:45:46 2004 +1000 +++ b/src/random-int.c Fri Jun 04 02:57:14 2004 +1000 @@ -6,30 +6,47 @@ * Public License as described at http://www.gnu.org/licenses/gpl.txt */ -#include <stdio.h> +#include <fcntl.h> #include <stdlib.h> +#include <unistd.h> #include <time.h> int random_int() { unsigned int seed; - FILE *devrandom; + int devrandom; + unsigned char ch; seed = (unsigned int)time(NULL); - devrandom = fopen("/dev/urandom", "r"); - if(!devrandom) - devrandom = fopen("/dev/random", "r"); + devrandom = open("/dev/urandom", O_RDONLY); + if(devrandom < 0) + devrandom = open("/dev/random", O_RDONLY); - if (devrandom) { - seed ^= ((unsigned char)fgetc(devrandom)); - seed ^= ((unsigned char)fgetc(devrandom)) << 8; - seed ^= ((unsigned char)fgetc(devrandom)) << 16; - seed ^= ((unsigned char)fgetc(devrandom)) << 24; - fclose(devrandom); + if (devrandom >= 0) { + read(devrandom, &ch, 1); + seed ^= ch; + read(devrandom, &ch, 1); + seed ^= ch << 8; + read(devrandom, &ch, 1); + seed ^= ch << 16; + read(devrandom, &ch, 1); + seed ^= ch << 24; + close(devrandom); } srand(seed); return rand(); } +#if 0 +int main(int argc, char **argv) +{ + int i; + + for(i = 0; i < 25; i++) + printf("%i\n", random_int()); + + return 0; +} +#endif