4 * author: hackbard@hackdaworld.dyndns.org
22 static u32 *c_ptr,*b_ptr;
24 int rand_init(char *rf)
29 if((rand_fd=open("/dev/urandom",O_RDONLY))<0)
31 puts("cannot open /dev/urandom");
37 if((rand_fd=open(rf,O_RDONLY))<0)
39 printf("cannot open %s\n",rf);
43 if((b_ptr=(u32 *)(malloc(BUFSIZE*sizeof(u32))))==NULL)
45 puts("failed allocating random buffer");
64 if(c_ptr>=b_ptr+BUFSIZE)
67 printf("getting another %d bytes of random data ...\n",BUFSIZE);
71 srand((int)time(NULL));
72 while(c_ptr<b_ptr+BUFSIZE) *(c_ptr++)=(u32)rand();
75 if(read(rand_fd,b_ptr,BUFSIZE*sizeof(u32))<BUFSIZE*sizeof(u32))
77 /* -> assume random file, end reached */
78 puts("warning, will read random file from beginning ...");
79 lseek(rand_fd,0,SEEK_SET);
80 read(rand_fd,b_ptr,BUFSIZE*sizeof(u32));
90 return((u32)(*(c_ptr++)*(max*1.0/((long long unsigned int)RAND_MAX+1))));
92 return((u32)(*(c_ptr++)*(max*1.0/((long long unsigned int)URAND_MAX+1))));
96 u32 get_rand_lgp(u32 max,double a,double b)
98 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));
101 u32 get_rand_reject(u32 max_x,u32 max_y,u32 *graph)
111 if(y<=graph[x]) ok=1;