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 boolean oneCached=FALSE;
41  double x1,x2,w;
42  double y1;
43  static double y2;
44 
45  if (oneCached)
46  {
47  oneCached=FALSE;
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=TRUE;
63  return(y1);
64  }
65 }
66 
68 {
69  double a,b;
70 
71  a=LowerLimit(t);
72  b=UpperLimit(t);
73 
74  return(a+randomDouble()*(b-a));
75 }
76 
77 unsigned int randomMax(unsigned int m)
78 {
79  if (m==0)
80  return(0);
81  else
82  return((unsigned int)floor(randomDouble()*(m+1))); /* in [0,m+1) -> all values in 0,m have same chances */
83 }
84 
85 
86 unsigned int randomWithDistribution(unsigned int m,double s,double *d)
87 {
88  double s1,r;
89  unsigned int i;
90 
91  if (s==0.0)
92  {
93  for(i=0;i<m;i++)
94  s+=d[i];
95  }
96 
97  r=s*randomDouble();
98 
99  i=0;
100  s1=d[0];
101  while((r>s1)&&(i<(m-1)))
102  {
103  i++;
104  s1+=d[i];
105  }
106 
107  return(i);
108 }
109 
110 void randomOnBall(double r,unsigned int k,double *p)
111 {
112  unsigned int i;
113  double n;
114 
115  n=0.0;
116  for(i=0;i<k;i++)
117  {
118  p[i]=randomNormal();
119  n+=(p[i]*p[i]);
120  }
121  n=r/sqrt(n);
122  for(i=0;i<k;i++)
123  p[i]*=n;
124 }
125 
126 void randomInBall(double r,unsigned int k,double *p)
127 {
128  unsigned int i;
129  double n;
130 
131  /* random on surface ... */
132  n=0.0;
133  for(i=0;i<k;i++)
134  {
135  p[i]=randomNormal();
136  n+=(p[i]*p[i]);
137  }
138  /* ... but scaling to select points inside the ball too. */
139  n=pow(randomDouble(),1/((double)k))*(r/sqrt(n));
140  for(i=0;i<k;i++)
141  p[i]*=n;
142 }
#define FALSE
FALSE.
Definition: boolean.h:30
void randomReset()
Resets the random seed.
Definition: random.c:18
double randomInInterval(Tinterval *t)
Returns a random double in the given interval.
Definition: random.c:67
#define TRUE
TRUE.
Definition: boolean.h:21
unsigned int randomWithDistribution(unsigned int m, double s, double *d)
Random number with a given discrete distribution.
Definition: random.c:86
double randomNormal()
Returns a random double acording to a normal distribution.
Definition: random.c:38
unsigned int randomMax(unsigned int m)
Returns a random integer in the range [0,m].
Definition: random.c:77
void randomSet(unsigned int seed)
Sets the random seed.
Definition: random.c:25
double LowerLimit(Tinterval *i)
Gets the lower limit.
Definition: interval.c:79
void randomInBall(double r, unsigned int k, double *p)
Random number in a k dimensional ball.
Definition: random.c:126
double UpperLimit(Tinterval *i)
Gets the uppser limit.
Definition: interval.c:87
void randomOnBall(double r, unsigned int k, double *p)
Random number on a k dimensional ball.
Definition: random.c:110
double randomDouble()
Returns a random double in the [0,1] interval.
Definition: random.c:33
Definition of basic randomization functions.
Defines a interval.
Definition: interval.h:33