The Cuik KD-Tree Library


random.c
Go to the documentation of this file.
1 #include "random.h"
2 
3 #include <sys/types.h>
4 #include <time.h>
5 #include <stdlib.h>
6 #include <math.h>
7 
19 {
20  srand((unsigned int)time(NULL));
21  rand();
22  rand();
23 }
24 
25 void randomSet(unsigned int seed)
26 {
27  srand(seed);
28  rand(); /*useful to avoid first rands to be almost equals*/
29  rand();
30 
31 }
32 
33 double randomDouble()
34 {
35  return((double)rand()/(double)RAND_MAX);
36 }
37 
38 double randomNormal()
39 {
40  static int oneCached=0;
41  double x1,x2,w;
42  double y1;
43  static double y2;
44 
45  if (oneCached)
46  {
47  oneCached=0;
48  return(y2);
49  }
50  else
51  {
52  do {
53  x1=2.0*randomDouble()-1.0;
54  x2=2.0*randomDouble()-1.0;
55  w=x1*x1+x2*x2;
56  } while (w>=1.0);
57 
58  w=sqrt((-2.0*log(w))/w);
59  y1=x1*w;
60  y2=x2*w;
61 
62  oneCached=1;
63  return(y1);
64  }
65 }
void randomReset()
Resets the random seed.
Definition: random.c:18
double randomNormal()
Returns a random double acording to a normal distribution.
Definition: random.c:38
void randomSet(unsigned int seed)
Sets the random seed.
Definition: random.c:25
double randomDouble()
Returns a random double in the [0,1] interval.
Definition: random.c:33
Definition of basic randomization functions.