X-Git-Url: https://hackdaworld.org/gitweb/?p=physik%2Fnlsop.git;a=blobdiff_plain;f=random.c;h=a664fa70de54f7cb1d14e39fd438751e2231ef1f;hp=ec99a33456689ff02bd2e4df1e204e53e27ec7b5;hb=21bb2d2c6e2d1de0e1c4a75d114a87a94b39ba8c;hpb=de3ae9b29a64eaf297b44665022665962eba4a14 diff --git a/random.c b/random.c index ec99a33..a664fa7 100644 --- a/random.c +++ b/random.c @@ -12,6 +12,10 @@ #include #include +#ifdef USE_LIBC_RAND +#include +#endif + #include "random.h" static int rand_fd; @@ -21,11 +25,13 @@ int rand_init(char *rf) { if(rf==NULL) { +#ifndef USE_LIBC_RAND if((rand_fd=open("/dev/urandom",O_RDONLY))<0) { puts("cannot open /dev/urandom"); return -1; } +#endif } else { if((rand_fd=open(rf,O_RDONLY))<0) @@ -46,7 +52,9 @@ int rand_init(char *rf) int rand_close(void) { +#ifndef USE_LIBC_RAND close(rand_fd); +#endif return 1; } @@ -55,6 +63,15 @@ u32 get_rand(u32 max) { if(c_ptr>=b_ptr+BUFSIZE) { +#ifdef MORE_PRINTF + printf("getting another %d bytes of random data ...\n",BUFSIZE); +#endif +#ifdef USE_LIBC_RAND + c_ptr=b_ptr; + srand((int)time(NULL)); + while(c_ptr assume random file, end reached */ @@ -63,9 +80,17 @@ u32 get_rand(u32 max) read(rand_fd,b_ptr,BUFSIZE*sizeof(u32)); } c_ptr=b_ptr; +#endif +#ifdef MORE_PRINTF + printf("got it!\n"); +#endif } +#ifdef USE_LIBC_RAND + return((u32)(*(c_ptr++)*(max*1.0/((long long unsigned int)RAND_MAX+1)))); +#else return((u32)(*(c_ptr++)*(max*1.0/((long long unsigned int)URAND_MAX+1)))); +#endif } u32 get_rand_lgp(u32 max,double a,double b) @@ -86,5 +111,5 @@ u32 get_rand_reject(u32 max_x,u32 max_y,u32 *graph) if(y<=graph[x]) ok=1; } - return y; + return x; }