aimsalgo  5.0.5
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 
38 #include <cstdlib>
40 #include <stdlib.h>
41 #include <time.h>
42 #include <math.h>
43 #include <complex>
44 
47 double UniformRandom();
50 double NormalRandom();
52 
55 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 
69 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 template <class T> class AimsData;
82 
85 AimsData<int> AimsRandomList( int size );
88 
89 template <class T> inline
90 T UniformRandom(const T &min,const T &max)
91 { return((T) ((double)min+(double)(max-min)*(double)UniformRandom()));
92 }
93 
94 
95 inline
96 short UniformRandom( const short& min, const short& max )
97 {
98  return min + short( UniformRandom() * (max-min+0.99999) );
99 }
100 
101 
102 inline
103 unsigned short
104 UniformRandom( const unsigned short& min, const unsigned short& max )
105 {
106  return min + (unsigned short)( UniformRandom() * (max-min+0.99999) );
107 }
108 
109 
110 inline
111 int UniformRandom( const int& min, const int& max )
112 {
113  return min + int( UniformRandom() * (max-min+0.99999) );
114 }
115 
116 
117 inline
118 unsigned int
119 UniformRandom( const unsigned int& min, const unsigned int& max )
120 {
121  return min + (unsigned int)( UniformRandom() * (max-min+0.99999) );
122 }
123 
124 
125 inline
126 long UniformRandom( const long& min, const long& max )
127 {
128  return min + long( UniformRandom() * (max-min+0.99999) );
129 }
130 
131 
132 inline
133 unsigned long
134 UniformRandom( const unsigned long& min, const unsigned long& max )
135 {
136  return min + (unsigned long)( UniformRandom() * (max-min+0.99999) );
137 }
138 
139 
140 template <class T> inline
141 T GaussianRandom(const T &mean,const T &sigma)
142 { return((T)((double)mean + (double)sigma * NormalRandom()));
143 }
144 
145 
146 
147 
148 
150 
151 
153 template< class T >
155 {
156 public:
157 
160  UniformDeviates() { }
163  virtual ~UniformDeviates() { }
165 
173  T ran0( int *idum=NULL );
174 
183  T ran1( int *idum=NULL );
184 
193  T ran2( int *idum=NULL );
194 
200  T ran3( int *idum=NULL );
201 };
202 
203 
204 template< class T > inline
206 {
207  int k;
208  T ans;
209  int idumm;
210 
211  if ( idum == NULL )
212  {
213  srand( time( NULL ) );
214  idumm = - rand();
215  }
216  else idumm = *idum;
217 
218  idumm ^= 123459876;
219  k = idumm / 127773;
220  idumm = 16807 * ( idumm - k * 127773 ) - 2836 * k;
221 
222  if ( idumm < 0 ) idumm += 2147483647;
223 
224  ans = (T)( ( 1.0 / 2147483647 ) * idumm );
225  idumm ^= 123459876;
226 
227  if ( idum ) *idum = idumm;
228 
229  return ans;
230 }
231 
232 
233 template< class T > inline
235 {
236  int j;
237  int k, idumm;
238  static int iy = 0;
239  static int iv[ 32 ];
240  T temp;
241 
242  if ( idum == NULL )
243  {
244  srand( time( NULL ) );
245  idumm = - rand();
246  }
247  else idumm = *idum;
248 
249  if ( idumm <= 0 || !iy )
250  {
251  if ( -idumm < 1 ) idumm = 1;
252  else idumm = -idumm;
253 
254  for ( j=39; j>=0; j-- )
255  {
256  k = idumm / 127773;
257  idumm = 16807 * ( idumm - k * 127773 ) - 2836 * k;
258 
259  if ( idumm < 0 ) idumm += 2147483647;
260  if ( j < 32 ) iv[ j ] = idumm;
261  }
262 
263  iy = iv[ 0 ];
264  }
265 
266  k = idumm / 127773;
267  idumm = 16807 * ( idumm - k * 127773 ) - 2836 * k;
268 
269  if ( idumm < 0 ) idumm += 2147483647;
270 
271  j = iy / ( 1 + 2147483646 / 32 );
272  iy = iv[ j ];
273  iv[ j ] = idumm;
274 
275  if ( idum ) *idum = idumm;
276 
277  if ( ( temp = (T)( 1.0 * iy / 2147483647 ) ) > (T)( 1.0 - 1.2e-7 ) )
278  return (T)( 1.0 - 1.2e-7 );
279  else return (T)temp;
280 }
281 
282 
283 template< class T > inline
285 {
286  int j;
287  int k, idumm;
288  static int idum2 = 123456789;
289  static int iy = 0;
290  static int iv[ 32 ];
291  T temp;
292 
293  if ( idum == NULL )
294  {
295  srand( time( NULL ) );
296  idumm = - rand();
297  }
298  else idumm = *idum;
299 
300  if ( idumm <= 0 )
301  {
302  if ( -idumm < 1 ) idumm = 1;
303  else idumm = -idumm;
304 
305  idum2 = idumm;
306 
307  for ( j = 39; j>=0; j-- )
308  {
309  k = idumm / 53668;
310  idumm = 40014 * ( idumm - k * 53668 ) - k * 12211;
311 
312  if ( idumm < 0 ) idumm += 2147483563;
313  if ( j < 32 ) iv[ j ] = idumm;
314  }
315 
316  iy = iv[ 0 ];
317  }
318 
319  k = idumm / 53668;
320  idumm = 40014 * ( idumm - k * 53668 ) - k * 12211;
321 
322  if ( idumm < 0 ) idumm += 2147483563;
323 
324  k = idum2 / 52774;
325  idum2 = 40692 * ( idum2 - k * 52774 ) - k * 3791;
326 
327  if ( idum2 < 0 ) idum2 += 2147483399;
328 
329  j = iy / ( 1 + 2147483562 / 32 );
330  iy = iv[ j ] - idum2;
331  iv[ j ] = idumm;
332  if ( iy < 1 ) iy += 2147483562;
333 
334  if ( idum ) *idum = idumm;
335 
336  if ( ( temp = (T)( 1.0 * iy / 2147483563 ) ) > (T)( 1.0 - 1.2e-7 ) )
337  return (T)( 1.0 - 1.2e-7 );
338  else return (T)temp;
339 }
340 
341 
342 template< class T > inline
344 {
345  static int inext, inextp;
346  static int ma[ 56 ];
347  static int iff = 0;
348  int mj, mk, idumm;
349  int i, ii, k;
350 
351  if ( idum == NULL )
352  {
353  srand( time( NULL ) );
354  idumm = - rand();
355  }
356  else idumm = *idum;
357 
358  if ( idumm < 0 || iff == 0 )
359  {
360  iff = 1;
361  mj = 161803398 - ( idumm < 0 ? -idumm : idumm );
362  mj %= 1000000000;
363  ma[ 55 ] = mj;
364  mk = 1;
365 
366  for ( i=1; i<=54; i++ )
367  {
368  ii = ( 21 * i ) % 55;
369  ma[ ii ] = mk;
370  mk = mj - mk;
371  if ( mk < 0 ) mk += 1000000000;
372  mj = ma[ ii ];
373  }
374 
375  for ( k=1; k<=4; k++ )
376  for ( i=1; i<=55; i++ )
377  {
378  ma[ i ] -= ma[ 1 + ( i + 30 ) % 55 ];
379 
380  if ( ma[ i ] < 0 ) ma[ i ] += 1000000000;
381  }
382 
383  inext = 0;
384  inextp = 31;
385  idumm = 1;
386  }
387 
388  if ( ++inext == 56 ) inext = 1;
389  if ( ++inextp == 56 ) inextp = 1;
390 
391  mj = ma[ inext ] - ma[ inextp ];
392 
393  if ( mj < 0 ) mj += 1000000000;
394 
395  ma[ inext ] = mj;
396 
397  if ( idum ) *idum = idumm;
398 
399  return (T)( 1.0 * mj / 1000000000 );
400 }
401 
402 
404 template< class T >
406 {
407 public:
408 
411  ExponentialDeviates() { }
414  virtual ~ExponentialDeviates() { }
416 
423  T doit( int *idum=NULL );
424 };
425 
426 
427 template< class T > inline
429 {
430  T dum;
432 
433  do dum = udev.ran1( idum );
434  while ( dum == (T)0 );
435 
436  return (T)( -log( (double)dum ) );
437 }
438 
439 
441 template< class T >
443 {
444 public:
445 
448  NormalDeviates() { }
451  virtual ~NormalDeviates() { }
453 
460  T doit( int *idum=NULL );
461 };
462 
463 
464 template< class T > inline
466 {
467  static int iset = 0;
468  static T gset;
469  T fac, rsq, v1, v2;
471 
472  if ( iset == 0 )
473  {
474  do
475  {
476  v1 = (T)( 2.0 * udev.ran1( idum ) - 1.0 );
477  v2 = (T)( 2.0 * udev.ran1( idum ) - 1.0 );
478  rsq = v1 * v1 + v2 * v2;
479  }
480  while ( rsq >= (T)1 || rsq == (T)0 );
481 
482  fac = (T)sqrt( -2.0 * log( (double)rsq ) / rsq );
483  gset = v1 * fac;
484  iset = 1;
485  return v2 * fac;
486  }
487  else
488  {
489  iset = 0;
490  return gset;
491  }
492 }
493 
494 #endif
float min(float x, float y)
Definition: thickness.h:105
T ran0(int *idum=NULL)
&#39;Minimal&#39; random number generator of Park and Miller.
Definition: random.h:205
Exponential random numbers.
Definition: random.h:405
float max(float x, float y)
Definition: thickness.h:97
double NormalRandom()
Normal gaussian distribution with mean = 0.0 and sigma = 1.0.
T GaussianRandom(const T &mean, const T &sigma)
generate gaussian random number with mean,sigma
Definition: random.h:141
T doit(int *idum=NULL)
Normal random numbers generator.
Definition: random.h:465
UniformDeviates()
Constructor and destructors.
Definition: random.h:161
T ran1(int *idum=NULL)
&#39;Minimal&#39; random number generator of Park and Miller with Bays-Durham shuffle and added safeguards...
Definition: random.h:234
double UniformRandom()
Uniform distribution between 0.0 and 1.0.
virtual ~ExponentialDeviates()
destructor
Definition: random.h:414
T ran3(int *idum=NULL)
Long period random number generator of Knuth.
Definition: random.h:343
AimsData< int > AimsRandomList(int size)
generate a random list of int between 0 and (size-1)
T doit(int *idum=NULL)
Exponential random number generator.
Definition: random.h:428
Normal random numbers.
Definition: random.h:442
T ran2(int *idum=NULL)
Long period random number generator of L&#39;Ecuyer with Bays-Derham shuffle and added safeguards...
Definition: random.h:284
virtual ~NormalDeviates()
destructor
Definition: random.h:451
virtual ~UniformDeviates()
destructor
Definition: random.h:163