+/* return random integer between 0 - max-1 */
+u32 rand_get(u32 max)
+{
+ if(rand_current>=rand_buf+(RAND_BUF_SIZE/sizeof(u32)))
+ {
+ printfd("debug: reading new random bytes\n");
+ if(read(random_fd,rand_buf,RAND_BUF_SIZE)!=RAND_BUF_SIZE)
+ {
+ if(!strcmp(random_file,""))
+ {
+ puts("random file end, starting over ...");
+ lseek(random_fd,0,SEEK_SET);
+ } else
+ {
+ puts("failed reading 1 mega bytes of random data");
+ return -23;
+ }
+ }
+ printfd("debug: finished reading random bytes\n");
+ rand_current=rand_buf;
+ }
+ /* cells numbered 0...max-1 */
+ return((u32)(*(rand_current++)*(max*1.0/URAND_MAX)));