projects
/
physik
/
nlsop.git
/ blobdiff
commit
grep
author
committer
pickaxe
?
search:
re
summary
|
shortlog
|
log
|
commit
|
commitdiff
|
tree
raw
|
inline
| side by side
support: meanvalue/sum of multiple fts
[physik/nlsop.git]
/
random.c
diff --git
a/random.c
b/random.c
index
17cbd73
..
a664fa7
100644
(file)
--- a/
random.c
+++ b/
random.c
@@
-9,6
+9,12
@@
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
+#include <stdlib.h>
+#include <math.h>
+
+#ifdef USE_LIBC_RAND
+#include <time.h>
+#endif
#include "random.h"
#include "random.h"
@@
-19,11
+25,13
@@
int rand_init(char *rf)
{
if(rf==NULL)
{
{
if(rf==NULL)
{
+#ifndef USE_LIBC_RAND
if((rand_fd=open("/dev/urandom",O_RDONLY))<0)
{
puts("cannot open /dev/urandom");
return -1;
}
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)
} else
{
if((rand_fd=open(rf,O_RDONLY))<0)
@@
-44,7
+52,9
@@
int rand_init(char *rf)
int rand_close(void)
{
int rand_close(void)
{
+#ifndef USE_LIBC_RAND
close(rand_fd);
close(rand_fd);
+#endif
return 1;
}
return 1;
}
@@
-53,6
+63,15
@@
u32 get_rand(u32 max)
{
if(c_ptr>=b_ptr+BUFSIZE)
{
{
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 */
if(read(rand_fd,b_ptr,BUFSIZE*sizeof(u32))<BUFSIZE*sizeof(u32))
{
/* -> assume random file, end reached */
@@
-61,9
+80,17
@@
u32 get_rand(u32 max)
read(rand_fd,b_ptr,BUFSIZE*sizeof(u32));
}
c_ptr=b_ptr;
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))));
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)
}
u32 get_rand_lgp(u32 max,double a,double b)
@@
-71,3
+98,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));
}
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;
+}