aimsalgo  5.1.2
Neuroimaging image processing
random.h
Go to the documentation of this file.
1 /* This software and supporting documentation are distributed by
2  * Institut Federatif de Recherche 49
3  * CEA/NeuroSpin, Batiment 145,
4  * 91191 Gif-sur-Yvette cedex
5  * France
6  *
7  * This software is governed by the CeCILL-B license under
8  * French law and abiding by the rules of distribution of free software.
9  * You can use, modify and/or redistribute the software under the
10  * terms of the CeCILL-B license as circulated by CEA, CNRS
11  * and INRIA at the following URL "http://www.cecill.info".
12  *
13  * As a counterpart to the access to the source code and rights to copy,
14  * modify and redistribute granted by the license, users are provided only
15  * with a limited warranty and the software's author, the holder of the
16  * economic rights, and the successive licensors have only limited
17  * liability.
18  *
19  * In this respect, the user's attention is drawn to the risks associated
20  * with loading, using, modifying and/or developing or reproducing the
21  * software by the user in light of its specific status of free software,
22  * that may mean that it is complicated to manipulate, and that also
23  * therefore means that it is reserved for developers and experienced
24  * professionals having in-depth computer knowledge. Users are therefore
25  * encouraged to load and test the software's suitability as regards their
26  * requirements in conditions enabling the security of their systems and/or
27  * data to be ensured and, more generally, to use and operate it in the
28  * same conditions as regards security.
29  *
30  * The fact that you are presently reading this means that you have had
31  * knowledge of the CeCILL-B license and that you accept its terms.
32  */
33 
34 
35 #ifndef AIMS_MATH_RANDOM_H
36 #define AIMS_MATH_RANDOM_H
37 
39 #include <cstdlib>
40 #include <stdlib.h>
41 #include <time.h>
42 #include <math.h>
43 #include <complex>
44 
48 double UniformRandom();
50 double NormalRandom();
52 
56 template <class T>
57 T UniformRandom(const T &min,const T &max);
59 std::complex<float> UniformRandom(const std::complex<float> &min,
60  const std::complex<float> &max);
62 std::complex<double> UniformRandom(const std::complex<double> &min,
63  const std::complex<double> &max);
65 
66 
70 template <class T>
71 T GaussianRandom(const T &mean,const T &sigma);
73 std::complex<float> GaussianRandom(const std::complex<float> &mean,
74  const std::complex<float> &sigma);
76 std::complex<double> GaussianRandom(const std::complex<double> &mean,
77  const std::complex<double> &sigma);
79 
80 
81 namespace carto
82 {
83  template <class T> class VolumeRef;
84 }
85 
91 
92 template <class T> inline
93 T UniformRandom(const T &min,const T &max)
94 { return((T) ((double)min+(double)(max-min)*(double)UniformRandom()));
95 }
96 
97 
98 inline
99 short UniformRandom( const short& min, const short& max )
100 {
101  return min + short( UniformRandom() * (max-min+0.99999) );
102 }
103 
104 
105 inline
106 unsigned short
107 UniformRandom( const unsigned short& min, const unsigned short& max )
108 {
109  return min + (unsigned short)( UniformRandom() * (max-min+0.99999) );
110 }
111 
112 
113 inline
114 int UniformRandom( const int& min, const int& max )
115 {
116  return min + int( UniformRandom() * (max-min+0.99999) );
117 }
118 
119 
120 inline
121 unsigned int
122 UniformRandom( const unsigned int& min, const unsigned int& max )
123 {
124  return min + (unsigned int)( UniformRandom() * (max-min+0.99999) );
125 }
126 
127 
128 inline
129 long UniformRandom( const long& min, const long& max )
130 {
131  return min + long( UniformRandom() * (max-min+0.99999) );
132 }
133 
134 
135 inline
136 unsigned long
137 UniformRandom( const unsigned long& min, const unsigned long& max )
138 {
139  return min + (unsigned long)( UniformRandom() * (max-min+0.99999) );
140 }
141 
142 
143 template <class T> inline
144 T GaussianRandom(const T &mean,const T &sigma)
145 { return((T)((double)mean + (double)sigma * NormalRandom()));
146 }
147 
148 
149 
150 
151 
153 
154 
156 template< class T >
158 {
159 public:
160 
166  virtual ~UniformDeviates() { }
168 
176  T ran0( int *idum=NULL );
177 
186  T ran1( int *idum=NULL );
187 
196  T ran2( int *idum=NULL );
197 
203  T ran3( int *idum=NULL );
204 };
205 
206 
207 template< class T > inline
209 {
210  int k;
211  T ans;
212  int idumm;
213 
214  if ( idum == NULL )
215  {
216  srand( time( NULL ) );
217  idumm = - rand();
218  }
219  else idumm = *idum;
220 
221  idumm ^= 123459876;
222  k = idumm / 127773;
223  idumm = 16807 * ( idumm - k * 127773 ) - 2836 * k;
224 
225  if ( idumm < 0 ) idumm += 2147483647;
226 
227  ans = (T)( ( 1.0 / 2147483647 ) * idumm );
228  idumm ^= 123459876;
229 
230  if ( idum ) *idum = idumm;
231 
232  return ans;
233 }
234 
235 
236 template< class T > inline
238 {
239  int j;
240  int k, idumm;
241  static int iy = 0;
242  static int iv[ 32 ];
243  T temp;
244 
245  if ( idum == NULL )
246  {
247  srand( time( NULL ) );
248  idumm = - rand();
249  }
250  else idumm = *idum;
251 
252  if ( idumm <= 0 || !iy )
253  {
254  if ( -idumm < 1 ) idumm = 1;
255  else idumm = -idumm;
256 
257  for ( j=39; j>=0; j-- )
258  {
259  k = idumm / 127773;
260  idumm = 16807 * ( idumm - k * 127773 ) - 2836 * k;
261 
262  if ( idumm < 0 ) idumm += 2147483647;
263  if ( j < 32 ) iv[ j ] = idumm;
264  }
265 
266  iy = iv[ 0 ];
267  }
268 
269  k = idumm / 127773;
270  idumm = 16807 * ( idumm - k * 127773 ) - 2836 * k;
271 
272  if ( idumm < 0 ) idumm += 2147483647;
273 
274  j = iy / ( 1 + 2147483646 / 32 );
275  iy = iv[ j ];
276  iv[ j ] = idumm;
277 
278  if ( idum ) *idum = idumm;
279 
280  if ( ( temp = (T)( 1.0 * iy / 2147483647 ) ) > (T)( 1.0 - 1.2e-7 ) )
281  return (T)( 1.0 - 1.2e-7 );
282  else return (T)temp;
283 }
284 
285 
286 template< class T > inline
288 {
289  int j;
290  int k, idumm;
291  static int idum2 = 123456789;
292  static int iy = 0;
293  static int iv[ 32 ];
294  T temp;
295 
296  if ( idum == NULL )
297  {
298  srand( time( NULL ) );
299  idumm = - rand();
300  }
301  else idumm = *idum;
302 
303  if ( idumm <= 0 )
304  {
305  if ( -idumm < 1 ) idumm = 1;
306  else idumm = -idumm;
307 
308  idum2 = idumm;
309 
310  for ( j = 39; j>=0; j-- )
311  {
312  k = idumm / 53668;
313  idumm = 40014 * ( idumm - k * 53668 ) - k * 12211;
314 
315  if ( idumm < 0 ) idumm += 2147483563;
316  if ( j < 32 ) iv[ j ] = idumm;
317  }
318 
319  iy = iv[ 0 ];
320  }
321 
322  k = idumm / 53668;
323  idumm = 40014 * ( idumm - k * 53668 ) - k * 12211;
324 
325  if ( idumm < 0 ) idumm += 2147483563;
326 
327  k = idum2 / 52774;
328  idum2 = 40692 * ( idum2 - k * 52774 ) - k * 3791;
329 
330  if ( idum2 < 0 ) idum2 += 2147483399;
331 
332  j = iy / ( 1 + 2147483562 / 32 );
333  iy = iv[ j ] - idum2;
334  iv[ j ] = idumm;
335  if ( iy < 1 ) iy += 2147483562;
336 
337  if ( idum ) *idum = idumm;
338 
339  if ( ( temp = (T)( 1.0 * iy / 2147483563 ) ) > (T)( 1.0 - 1.2e-7 ) )
340  return (T)( 1.0 - 1.2e-7 );
341  else return (T)temp;
342 }
343 
344 
345 template< class T > inline
347 {
348  static int inext, inextp;
349  static int ma[ 56 ];
350  static int iff = 0;
351  int mj, mk, idumm;
352  int i, ii, k;
353 
354  if ( idum == NULL )
355  {
356  srand( time( NULL ) );
357  idumm = - rand();
358  }
359  else idumm = *idum;
360 
361  if ( idumm < 0 || iff == 0 )
362  {
363  iff = 1;
364  mj = 161803398 - ( idumm < 0 ? -idumm : idumm );
365  mj %= 1000000000;
366  ma[ 55 ] = mj;
367  mk = 1;
368 
369  for ( i=1; i<=54; i++ )
370  {
371  ii = ( 21 * i ) % 55;
372  ma[ ii ] = mk;
373  mk = mj - mk;
374  if ( mk < 0 ) mk += 1000000000;
375  mj = ma[ ii ];
376  }
377 
378  for ( k=1; k<=4; k++ )
379  for ( i=1; i<=55; i++ )
380  {
381  ma[ i ] -= ma[ 1 + ( i + 30 ) % 55 ];
382 
383  if ( ma[ i ] < 0 ) ma[ i ] += 1000000000;
384  }
385 
386  inext = 0;
387  inextp = 31;
388  idumm = 1;
389  }
390 
391  if ( ++inext == 56 ) inext = 1;
392  if ( ++inextp == 56 ) inextp = 1;
393 
394  mj = ma[ inext ] - ma[ inextp ];
395 
396  if ( mj < 0 ) mj += 1000000000;
397 
398  ma[ inext ] = mj;
399 
400  if ( idum ) *idum = idumm;
401 
402  return (T)( 1.0 * mj / 1000000000 );
403 }
404 
405 
407 template< class T >
409 {
410 public:
411 
417  virtual ~ExponentialDeviates() { }
419 
426  T doit( int *idum=NULL );
427 };
428 
429 
430 template< class T > inline
432 {
433  T dum;
435 
436  do dum = udev.ran1( idum );
437  while ( dum == (T)0 );
438 
439  return (T)( -log( (double)dum ) );
440 }
441 
442 
444 template< class T >
446 {
447 public:
448 
454  virtual ~NormalDeviates() { }
456 
463  T doit( int *idum=NULL );
464 };
465 
466 
467 template< class T > inline
469 {
470  static int iset = 0;
471  static T gset;
472  T fac, rsq, v1, v2;
474 
475  if ( iset == 0 )
476  {
477  do
478  {
479  v1 = (T)( 2.0 * udev.ran1( idum ) - 1.0 );
480  v2 = (T)( 2.0 * udev.ran1( idum ) - 1.0 );
481  rsq = v1 * v1 + v2 * v2;
482  }
483  while ( rsq >= (T)1 || rsq == (T)0 );
484 
485  fac = (T)sqrt( -2.0 * log( (double)rsq ) / rsq );
486  gset = v1 * fac;
487  iset = 1;
488  return v2 * fac;
489  }
490  else
491  {
492  iset = 0;
493  return gset;
494  }
495 }
496 
497 #endif
Exponential random numbers.
Definition: random.h:409
T doit(int *idum=NULL)
Exponential random number generator.
Definition: random.h:431
ExponentialDeviates()
Constructor and destructor.
Definition: random.h:415
virtual ~ExponentialDeviates()
destructor
Definition: random.h:417
Normal random numbers.
Definition: random.h:446
NormalDeviates()
Constructors and destructors.
Definition: random.h:452
virtual ~NormalDeviates()
destructor
Definition: random.h:454
T doit(int *idum=NULL)
Normal random numbers generator.
Definition: random.h:468
T ran0(int *idum=NULL)
'Minimal' random number generator of Park and Miller.
Definition: random.h:208
T ran1(int *idum=NULL)
'Minimal' random number generator of Park and Miller with Bays-Durham shuffle and added safeguards.
Definition: random.h:237
T ran3(int *idum=NULL)
Long period random number generator of Knuth.
Definition: random.h:346
virtual ~UniformDeviates()
destructor
Definition: random.h:166
UniformDeviates()
Constructor and destructors.
Definition: random.h:164
T ran2(int *idum=NULL)
Long period random number generator of L'Ecuyer with Bays-Derham shuffle and added safeguards.
Definition: random.h:287
float min(float x, float y)
Definition: thickness.h:106
float max(float x, float y)
Definition: thickness.h:98
double NormalRandom()
Normal gaussian distribution with mean = 0.0 and sigma = 1.0.
carto::VolumeRef< int > AimsRandomList(int size)
generate a random list of int between 0 and (size-1)
T GaussianRandom(const T &mean, const T &sigma)
generate gaussian random number with mean,sigma
Definition: random.h:144
double UniformRandom()
Uniform distribution between 0.0 and 1.0.