X-Git-Url: https://hackdaworld.org/gitweb/?p=physik%2Fnlsop.git;a=blobdiff_plain;f=random.c;h=468a3a5d877cd329801d2546048dd572a14e9bd0;hp=4d96243786f5aab2fec4d74b42fea35d02736e1f;hb=f9a7f27f952d23ebd856c4c60fbdf7c024f95f7b;hpb=cae1c91bf6b8d41d5a436f48f6e18514eeedbfb9 diff --git a/random.c b/random.c index 4d96243..468a3a5 100644 --- a/random.c +++ b/random.c @@ -10,6 +10,7 @@ #include #include #include +#include #include "random.h" @@ -54,6 +55,9 @@ 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 if(read(rand_fd,b_ptr,BUFSIZE*sizeof(u32)) assume random file, end reached */ @@ -62,6 +66,9 @@ u32 get_rand(u32 max) read(rand_fd,b_ptr,BUFSIZE*sizeof(u32)); } c_ptr=b_ptr; +#ifdef MORE_PRINTF + printf("got it!\n"); +#endif } return((u32)(*(c_ptr++)*(max*1.0/((long long unsigned int)URAND_MAX+1)))); @@ -72,3 +79,18 @@ u32 get_rand_lgp(u32 max,double a,double b) return((u32)(1.0*max*(-1.0*b+sqrt(b*b+2*a*((b+a/2)*get_rand(URAND_MAX)/((long long unsigned int)URAND_MAX+1))))/a)); } +u32 get_rand_reject(u32 max_x,u32 max_y,u32 *graph) +{ + u32 x,y; + unsigned char ok; + + ok=0; + while(!ok) + { + x=get_rand(max_x); + y=get_rand(max_y); + if(y<=graph[x]) ok=1; + } + + return x; +}