#include <stdlib.h>
#include <math.h>
+#ifdef USE_LIBC_RAND
+#include <time.h>
+#endif
+
#include "random.h"
static int rand_fd;
{
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)
int rand_close(void)
{
+#ifndef USE_LIBC_RAND
close(rand_fd);
+#endif
return 1;
}
{
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<b_ptr+BUFSIZE) *(c_ptr++)=(u32)rand();
+ c_ptr=b_ptr;
+#else
if(read(rand_fd,b_ptr,BUFSIZE*sizeof(u32))<BUFSIZE*sizeof(u32))
{
/* -> assume random file, end reached */
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)