2 * random.c - functions to get random values
10 #include <sys/types.h>
15 /* global and extern variables */
18 extern u32 *rand_buf,*rand_current;
19 extern char random_file[MAX_CHARS_RANDOM_FILE];
21 /* return random integer between 0 - max-1 */
24 if(rand_current>=rand_buf+(RAND_BUF_SIZE/sizeof(u32)))
26 printfd("debug: reading new random bytes\n");
27 if(read(random_fd,rand_buf,RAND_BUF_SIZE)!=RAND_BUF_SIZE)
29 if(!strcmp(random_file,""))
31 puts("random file end, starting over ...");
32 lseek(random_fd,0,SEEK_SET);
35 puts("failed reading 1 mega bytes of random data");
39 printfd("debug: finished reading random bytes\n");
40 rand_current=rand_buf;
42 /* cells numbered 0...max-1 */
43 return((u32)(*(rand_current++)*(max*1.0/URAND_MAX)));
46 /* get z value (linear growth of probability with depths) */
47 u32 rand_get_lgp(int slope,int start,u32 z_max)
51 weighted_sum_z=z_max*start+slope*gr;
52 z=rand_get(weighted_sum_z)+1; /* +1 as rand_get returns values 0...max-1 */
58 return(i-1); /* return values 0...z_cell-1 */