Basic Image AlgorithmS Library  2.8.0
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
Random.cpp
1 /*
2 This file is part of the BIAS library (Basic ImageAlgorithmS).
3 
4 Copyright (C) 2003-2009 (see file CONTACT for details)
5  Multimediale Systeme der Informationsverarbeitung
6  Institut fuer Informatik
7  Christian-Albrechts-Universitaet Kiel
8 
9 
10 BIAS is free software; you can redistribute it and/or modify
11 it under the terms of the GNU Lesser General Public License as published by
12 the Free Software Foundation; either version 2.1 of the License, or
13 (at your option) any later version.
14 
15 BIAS is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU Lesser General Public License for more details.
19 
20 You should have received a copy of the GNU Lesser General Public License
21 along with BIAS; if not, write to the Free Software
22 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 */
24 
25 
26 #include "Random.hh"
27 
28 #ifdef WIN32
29 #include <Base/Common/W32Compat.hh>
30 #endif //_WIN32
31 
32 #include <cmath>
33 #include <cstdlib>
34 #include <stdlib.h>
35 #include <float.h>
36 
37 #ifdef BIAS_HAVE_GSL
38 #include <gsl/gsl_version.h>
39 #include <gsl/gsl_rng.h>
40 #include <gsl/gsl_randist.h>
41 #endif
42 using namespace BIAS;
43 using namespace std;
44 
45 //#define RANDOM_SEED_ZERO_FIXED
46 
47 // #ifdef RANDOM_SEED_ZERO_FIXED
48 // #include <fstream>
49 // namespace BIAS {
50 // int rand(void){
51 // static std::ofstream os("random.txt");
52 // if (!os.good()){ BIASERR("error writing to file "); BIASABORT; }
53 // static int num = 0;
54 // int ran = ::rand();
55 // cout << setw(5) << num << setw(15) << ran << endl << flush;
56 // os << setw(5) << num << setw(15) << ran << endl << flush;
57 // num++;
58 // return ran;
59 // }
60 // }
61 //#endif
62 
65 {}
66 
67 
70  : Seed_(0), Idum_(0), LongIdum_(0)
71 {
72  // to avoid nondeterministics by only instantiating a new object
73  // never call srand() here
74  // evers 2005-06-30
75  // Reset();
76 #ifdef RANDOM_SEED_ZERO_FIXED
77  Reset(0);
78 #endif
79 }
80 
82 Random(const long int seed)
83  : Seed_(0), Idum_(0), LongIdum_(0)
84 {
85  Reset(seed);
86 }
87 
88 #ifdef RANDOM_SEED_ZERO_FIXED
89 static bool first = true;
90 #endif
91 
92 void Random::Reset(const long int seed)
93 {
94 #ifdef RANDOM_SEED_ZERO_FIXED
95  if (!first){
96  return;
97  }
98  first = false;
99  Seed_ = 0;
100 #else
101  Seed_ = seed;
102 #endif
103  //cout << "srand("<<Seed_<<")\n";
104  // initialize random source
105  srand((long int)Seed_);
106  // initialize seed for ran1_
107  Idum_=rand();
108  LongIdum_ = (long)Idum_;
109  //cerr << "Idum_: " << Idum_ << "\tLongIdum_: "<<LongIdum_<<endl;
110 }
111 
112 void Random::
114 {
115 #ifdef RANDOM_SEED_ZERO_FIXED
116  if (!first){
117  return;
118  }
119  first = false;
120  Seed_ = 0;
121 #else
122  //long int uc;
123  time_t uc;
124 
125  // get the seed (seconds since 1.1.1970 0:00)
126  time(&uc);
127  Seed_=(unsigned int)(uc);
128 #endif
129  //cout << "srand("<<Seed_<<")\n";
130  // initialize random source
131  srand( Seed_ );
132  // initialize seed for ran1_
133  Idum_=rand();
134  LongIdum_ = (long)Idum_;
135  //cerr << "Idum_: " << Idum_ << "\tLongIdum_: "<<LongIdum_<<endl;
136 }
137 
138 int Random::
139 GetUniformDistributedInt(const int min_arg, const int max_arg)
140 {
141  int min = (min_arg<max_arg)?(min_arg):(max_arg);
142  int max = (min_arg<max_arg)?(max_arg):(min_arg);
143  long int r;
144 #ifdef WIN32
145  r = rand();
146 #else
147  r = random();
148 #endif
149  double nr = (double)r / (double)(RAND_MAX);
150  int val = min + (int)(nr * (double)(max-min));
151  //int old_val = int(min+(double(rand())/(RAND_MAX)*(max-min+1)));
152  BIASASSERT(val>=min && val<=max);
153  return val;
154 }
155 
156 unsigned int Random::
157 GetUniformDistributedInt(const unsigned min_arg, const unsigned max_arg)
158 {
159  unsigned min = (min_arg<max_arg)?(min_arg):(max_arg);
160  unsigned max = (min_arg<max_arg)?(max_arg):(min_arg);
161 // double bin = (double)(1+max-min);
162 // double res = GetUniformDistributed(0.0, 1.0) * bin;
163 // //cerr << setprecision(14) << res<<" "<<(unsigned int)floor(res)+min << endl;
164 // return (unsigned int)floor(res)+min;
165  double dval = GetUniformDistributed((double)min, (double)max+1.0);
166  unsigned val = (unsigned)(dval);
167  if (val<min || val>max) val = (val % (max+1-min)) + min;
168  BIASASSERT(val>=min && val<=max);
169  return val;
170 }
171 
172 
173 void Random::
174 GetUniformDistributed(const double min, const double max, const int num,
175  vector<double>& res)
176 {
177  res.resize(num);
178  double size=max-min;
179  for (int i=0; i<num; i++)
180  res[i]=min + ran2_(&LongIdum_) * size;
181 }
182 
183 double Random::
184 GetNormalDistributedGSL(const double mean, const double sigma)
185 {
186 #ifdef BIAS_HAVE_GSL
187  static bool initialized = false;
188  static gsl_rng * r = NULL;
189  if (!initialized){
190  cout << "initializing gsl_rng ! \n";
191  gsl_rng_env_setup();
192  // const gsl_rng_type *T = gsl_rng_taus2;
193  const gsl_rng_type * T = gsl_rng_mt19937;
194  BIASASSERT(T);
195  r = gsl_rng_alloc (T);
196  BIASASSERT(r);
197  initialized = true;
198  }
199 
200  return gsl_ran_gaussian(r, sigma) + mean;
201 
202  // this is missing
203  // gsl_rng_free (r);
204  //}
205 #else
206  BIASERR("GetNormalDistributedGSL(): No GSL support in BIAS, returning "
207  <<"own implementation");
208  return GetNormalDistributed(mean, sigma);
209 #endif
210 }
211 
212 void Random::
213 GetNormalDistributed(const double mean, const double sigma,
214  const int num, std::vector<double>& res)
215 {
216  res.resize(num);
217  for (int i=0; i<num; i++)
218  res[i]=mean + gasdevf_(&LongIdum_) * sigma;
219 }
220 
221 
222 
223 int Random::
225  Vector2<double>& ran)
226 {
227  int res=0;
228 
229  vector<Vector2<double> > vran;
230  res = GetNormalDistributed(mean, cov, 1, vran);
231 
232  if (res==0) ran = vran[1];
233 
234  return res;
235 }
236 
237 int Random::
239  const Matrix2x2<double>& cov,
240  const int num, vector<Vector2<double> >& ran)
241 {
242  int res=0;
243  Vector2<double> eigen_vec1, eigen_vec2, tmp_ran;
244  double eigen_val1, eigen_val2;
245  int eigen_res =
246  cov.EigenvalueDecomposition(eigen_val1,eigen_vec1, eigen_val2, eigen_vec2);
247 
248  //cout << "eignevalues of covariance "<<eigen_val1<<", "<<eigen_val2<<endl;
249  //cout << "eignevectors of covariance "<<eigen_vec1<<", "<<eigen_vec2<<endl;
250 
252  R[0][0] = eigen_vec1[0];
253  R[1][0] = eigen_vec1[1];
254  R[0][1] = eigen_vec2[0];
255  R[1][1] = eigen_vec2[1];
256  //cout << "R: "<<R<<endl;
257 
258  vector<double> ran1, ran2;
259  switch (eigen_res){
260  case 2:
261  //cout << "2D distribution" << endl;
262  GetNormalDistributed(0.0, sqrt(eigen_val1), num, ran1);
263  GetNormalDistributed(0.0, sqrt(eigen_val2), num, ran2);
264  break;
265  case 1:
266  //cout << "pseudo 2D distribution = 1D dsitribution" << endl;
267  GetNormalDistributed(0.0, sqrt(eigen_val1), num, ran1);
268  ran2.resize(num, 0.0);
269  break;
270  case 0:
271  BIASERR("not a covariance matrix ("<<eigen_res<<") "<<cov<<endl);
272  //BIASASSERT(false);
273  BIASABORT;
274  //res = -1;
275  break;
276  default:
277  res = eigen_res;
278  break;
279  }
280 
281  if (res>=0){
282  ran.resize(num);
283  vector<Vector2<double> >::iterator it;
284  for (int i=(int)ran.size()-1; i>=0; i--){
285  ran[i].Set(ran1[i], ran2[i]);
286  //cout <<"---\nraw: "<< ran[i]<<endl;
287  ran[i] = R * ran[i];
288  //cout <<"rot: "<< ran[i]<<endl;
289  ran[i] += mean;
290  //cout <<"sht: "<< ran[i]<<endl;
291 
292  }
293  }
294 
295  return res;
296 }
297 
298 
299 int Random::
300 GetQuasiUniformDistributed(const int req_dim,
301  const Vector<double>& min,
302  const Vector<double>& max,
303  Vector<double>& res)
304 {
305  if (req_dim>6){
306  BIASERR("quasi random sequences can be only generated for spaces with "
307  "dimension less than 7. Requested dimension is "<<req_dim);
308  return -2;
309  }
310  static int dimension = -1;
311  static double r[6];
312  if (dimension != req_dim || req_dim<=0){
313  dimension = -1;
314  // initialize
315  // cout << "reinitializing quasi random number sequence generator\n";
316  sobseq_(&dimension, &r[0]);
317  dimension = req_dim;
318  }
319  if (req_dim<=0){ // just initialization is requested
320  return 0;
321  }
322  if (min.Size()!=max.Size() || req_dim!=(int)min.Size()){
323  BIASERR("argument vector size mismatch: "<<min.Size()<<" "<<max.Size()
324  <<" "<<req_dim);
325  return -1;
326  }
327  for (int d=0; d<req_dim; d++){
328  if (max[d]<min[d]) {
329  BIASERR("max["<<d<<"] smaller than min["<<d<<"] ("<<max[d]
330  <<" < "<<min[d]<<")");
331  return -4;
332  }
333  }
334 
335  sobseq_(&dimension, r);
336  if (res.Size() != min.Size())
337  res.newsize(dimension);
338 
339  const Vector<double> scale = max - min;
340 
341  for (int i=0; i<dimension; i++)
342  res[i] = r[i] * scale[i] + min[i];
343  return 0;
344 }
345 
346 
347 int Random::
348 GetQuasiUniformDistributed(const int req_dim,
349  const Vector<double>& min,
350  const Vector<double>& max,
351  const unsigned num,
352  std::vector<Vector<double> >& res)
353 {
354  for (int d=0; d<req_dim; d++){
355  if (max[d]<min[d]) {
356  BIASERR("max["<<d<<"] smaller than min["<<d<<"] ("<<max[d]
357  <<" < "<<min[d]<<")");
358  return -3;
359  }
360  }
361  if (min.Size()!=max.Size() || req_dim!=(int)min.Size()){
362  BIASERR("argument vector size mismatch: "<<min.Size()<<" "<<max.Size()
363  <<" "<<req_dim);
364  return -1;
365  }
366  if (num==0){
367  res.clear();
368  return 0;
369  }
370  double r[7];
371  int dimension;
372  const int num_runs = req_dim/6 + (req_dim%6==0)?(0):(1);
373  const Vector<double> scale = max - min;
374  Vector<double> zero(req_dim, 0.0);
375  res.resize(num, zero);
376  for (int k=0; k<num_runs; k++){
377  int k6 = k*6;
378  dimension = -1;
379  // initialize
380  sobseq_(&dimension, &r[0]);
381  dimension = (k==(num_runs-1))?(req_dim%6):(6);
382  for (unsigned i=0; i<num; i++){
383  sobseq_(&dimension, &r[0]);
384  for (int d=0; d<dimension; d++){
385  res[i][k6+d] = r[d] * scale[k6+d] + min[k6+d];
386  //cout << "r[d]: "<<r[d]<<"\tres: "<<res[i][k6+d]<<endl;
387  INFNANCHECK(r[d]);
388  INFNANCHECK(res[i][k6+d]);
389  }
390  }
391  }
392  return 0;
393 }
394 
395 int Random::
397 {
398  if (dim>6){
399  BIASERR("quasi random sequences can be only generated for spaces with "
400  "dimension less than 7. Requested dimension is "<<res.Size());
401  return -1;
402  }
403  if (dim<=0){
404  BIASERR("invalid dimension: "<<dim);
405  return -2;
406  }
407 
408  Vector<double> uniform(dim), min(dim), max(dim);
409  for (int i=0; i<dim; i++){
410  min[i] = -1.0;
411  max[i] = +1.0;
412  }
413 
414  int mres = 0;
415  GetQuasiUniformDistributed(dim, min, max, uniform);
416  for (int i=0; i<dim; i++){
417  if ((mres = Uniform2Normal_(uniform[i], res[i]))!=0){
418  return mres;
419  }
420  }
421  return 0;
422 }
423 
424 int Random::
425 GetQuasiNormalDistributed(const int dim, const unsigned num,
426  std::vector<Vector<double> >& res)
427 {
428  if (dim<=0){
429  BIASERR("invalid dimension: "<<dim);
430  return -2;
431  }
432 
433  Vector<double> min(dim), max(dim);
434  for (int i=0; i<dim; i++){
435  min[i] = -1.0;
436  max[i] = +1.0;
437  }
438 
439  int mres = GetQuasiUniformDistributed(dim, min, max, num, res);
440  if (mres!=0) return mres;
441  BIASASSERT(res.size()==(unsigned)num);
442  for (unsigned i=0; i<num; i++){
443  for (int d=0; d<dim; d++){
444  //cout << "uniform: "<<res[i][d];
445  if ((mres = Uniform2Normal_(res[i][d], res[i][d]))!=0){
446  return mres;
447  }
448  //cout << "\tnormal: "<<res[i][d]<<endl;
449  }
450  }
451  return 0;
452 }
453 
454 
455 int Random::
456 Uniform2Normal_(const double src, double& dst) const
457 {
458  if (src<-1.0 || src>1.0) return -1;
459  const double abs_src = fabs(src);
460  if (src==0.0) {
461  //static const double sqrt_2_pi =
462  // 2.5066282746310005024157652848110452530069867406099383166299235763422937;
463  dst = 0.0;
464  } else {
465  static const double sqrt_2 =
466  1.4142135623730950488016887242096980785696718753769480731766797379907325;
467 
468  dst = sqrt(-2.0 * log(abs_src)) / sqrt_2;
469  if (src<0.0) dst = -dst;
470  }
471  INFNANCHECK(dst);
472  return 0;
473 }
474 
475 
476 int Random::
477 Uniform2Normal_(const double src1, const double src2,
478  double& dst1, double& dst2) const
479 {
480  if (src1<-1.0 || src1>1.0) return -2;
481  if (src2<-1.0 || src2>1.0) return -2;
482  double rsq = src1*src1 + src2*src2;
483  if (rsq>=1.0 || rsq==0.0) return -1;
484  double fac = sqrt(-2.0 * log(rsq) / rsq);
485  dst1 = src1 * fac;
486  dst2 = src2 * fac;
487  INFNANCHECK(dst1);
488  INFNANCHECK(dst2);
489  return 0;
490 }
491 
492 // from www.nr.com copied from pdf
493 #define IA 16807
494 #define IM 2147483647
495 #define AM (1.0/IM)
496 #define IQ 127773
497 #define IR 2836
498 #define NTAB 32
499 #define NDIV (1+(IM-1)/NTAB)
500 #define EPS DBL_EPSILON
501 #define RNMX (1.0-EPS)
502 double Random::
503 ran1_(int *idum)
504 {
505  int j;
506  long k;
507  static long iy=0;
508  static long iv[NTAB];
509  float temp;
510 
511  if (*idum <= 0 || !iy){
512  if (-(*idum) < 1) *idum = 1;
513  else *idum = -(*idum);
514  for (j=NTAB+7; j>=0; j--){
515  k=(*idum)/IQ;
516  *idum=IA*(*idum-k*IQ)-IR*k;
517  if (*idum <0) *idum += IM;
518  if (j<NTAB) iv[j] = *idum;
519  }
520  iy=iv[0];
521  }
522  k=(*idum)/IQ;
523  *idum=IA*(*idum-k*IQ)-IR*k;
524  if (*idum < 0) *idum += IM;
525  j=iy/NDIV;
526  iy=iv[j];
527  iv[j] = *idum;
528  if ((temp=float(AM*iy)) > RNMX) return RNMX;
529  else return temp;
530 }
531 #undef IA
532 #undef IM
533 #undef AM
534 #undef IQ
535 #undef IR
536 #undef NTAB
537 #undef NDIV
538 #undef EPS
539 #undef RNMX
540 
541 //from D_RAN1.c
542 #define M1 259200
543 #define IA1 7141
544 #define IC1 54773
545 #define RM1 (1.0/M1)
546 #define M2 134456
547 #define IA2 8121
548 #define IC2 28411
549 #define RM2 (1.0/M2)
550 #define M3 243000
551 #define IA3 4561
552 #define IC3 51349
553 double Random::
554 ran1c_(int *idum)
555 {
556  static long ix1,ix2,ix3;
557  static double r[98];
558  double temp;
559  static int iff=0;
560  int j;
561  //void nrerror();
562 
563  if (*idum < 0 || iff == 0) {
564  iff=1;
565  ix1=(IC1-(*idum)) % M1;
566  ix1=(IA1*ix1+IC1) % M1;
567  ix2=ix1 % M2;
568  ix1=(IA1*ix1+IC1) % M1;
569  ix3=ix1 % M3;
570  for (j=1;j<=97;j++) {
571  ix1=(IA1*ix1+IC1) % M1;
572  ix2=(IA2*ix2+IC2) % M2;
573  r[j]=(ix1+ix2*RM2)*RM1;
574  }
575  *idum=1;
576  }
577  ix1=(IA1*ix1+IC1) % M1;
578  ix2=(IA2*ix2+IC2) % M2;
579  ix3=(IA3*ix3+IC3) % M3;
580  j=(int)rint(1 + ((97.0*ix3)/M3));
581  if (j > 97 || j < 1) BIASERR("RAN1: This cannot happen. "<<j);
582  temp=r[j];
583  r[j]=(ix1+ix2*RM2)*RM1;
584  return temp;
585 }
586 #undef M1
587 #undef IA1
588 #undef IC1
589 #undef RM1
590 #undef M2
591 #undef IA2
592 #undef IC2
593 #undef RM2
594 #undef M3
595 #undef IA3
596 #undef IC3
597 
598 // from D_RAN2.c
599 #define M 714025
600 #define IA 1366
601 #define IC 150889
602 double Random::
603 ran2_(long *idum)
604 {
605  //cout << "idum "<<*idum<<endl;
606  static long iy=0, ir[98];
607  static int iff=0;
608  int j;
609 
610  BIASASSERT(idum!=NULL);
611  if ((*idum) < 0 || iff == 0)
612  {
613  iff=1;
614  if (((*idum)=(IC-(*idum)) % M) < 0) *idum = -(*idum);
615  for (j=1;j<=97;j++) {
616  *idum=(IA*(*idum)+IC) % M;
617  ir[j]=(*idum);
618  }
619  (*idum) = (IA*(*idum)+IC) % M;
620  iy=(*idum);
621  }
622  j = (int)floor(1 + 97.0*iy / M);
623  //if (j > 97 || j < 1) BIASERR("RAN2: This should not happen (j="<<j<<")!");
624  if (j < 0) j = -j;
625  if (j > 97 || j < 1) j = (j % 97) + 1;
626  iy=ir[j];
627  *idum=(IA*(*idum)+IC) % M;
628  ir[j]=(*idum);
629  return (double) iy/M;
630 }
631 #undef M
632 #undef IA
633 #undef IC
634 
635 // from D_GASDEV.c
636 double Random::
637 gasdev_(int *idum)
638 {
639  static int iset=0;
640  static double gset;
641  double fac,r,v1,v2;
642 
643  if (iset == 0) {
644  do {
645  v1=2.0*ran1_(idum)-1.0;
646  v2=2.0*ran1_(idum)-1.0;
647  r=v1*v1+v2*v2;
648  } while (r >= 1.0);
649  fac=sqrt(-2.0*log(r)/r);
650  gset=v1*fac;
651  iset=1;
652  return v2*fac;
653  } else {
654  iset=0;
655  return gset;
656  }
657 }
658 
659 double Random::
660 gasdevf_(long *idum)
661 {
662  static int iset=0;
663  static double gset;
664  double fac,r,v1,v2;
665 
666  if (iset == 0) {
667  do {
668  v1=2.0*ran2_(idum)-1.0;
669  v2=2.0*ran2_(idum)-1.0;
670  r=v1*v1+v2*v2;
671  } while (r >= 1.0);
672  fac=sqrt(-2.0*log(r)/r);
673  gset=v1*fac;
674  iset=1;
675  return v2*fac;
676  } else {
677  iset=0;
678  return gset;
679  }
680 }
681 
682 // from makenois.c
683 double Random::
684 gasdev3_(int *idum)
685 {
686  static int iset=1;
687  static double gset;
688  double fac,r,v1,v2;
689 
690  if (iset) {
691  do {
692  v1=2.0*ran1_(idum)-1.0;
693  v2=2.0*ran1_(idum)-1.0;
694  r=v1*v1+v2*v2;
695  } while (r >= 1.0);
696  fac=sqrt(-2.0*log(r)/r);
697  gset = v1*fac;
698  iset = 0;
699  return v2*fac;
700  } else {
701  iset = 1;
702  return gset;
703  }
704 }
705 
706 void Random::GetMeanAndCovariance(const std::vector<double>& samples,
707  double &mean, double &covariance) const {
708  mean = 0;
709  for (unsigned int i=0; i<samples.size(); i++) {
710  mean += samples[i];
711  }
712  covariance = 0;
713  if (samples.empty()) return;
714  mean /= samples.size();
715  for (unsigned int i=0; i<samples.size(); i++) {
716  covariance += (mean - samples[i])*(mean - samples[i]);
717  }
718  covariance /= samples.size();
719 }
720 
721 
723  >&samples,
724  BIAS::Vector<double> &mean,
725  BIAS::Matrix<double> &covariance) const {
726  BIASASSERT(!samples.empty());
727  const int dim = samples[0].Size();
728  mean.newsize(dim);
729  for (unsigned int i=0; i<samples.size(); i++) {
730  mean += samples[i];
731  }
732  mean /= (double)samples.size();
733 
734  covariance.newsize(dim,dim);
735  covariance.SetZero();
736  for (unsigned int i=0; i<samples.size(); i++) {
737  covariance +=
738  BIAS::Vector<double>(mean - samples[i]).OuterProduct((mean -
739  samples[i]));
740  }
741  covariance *= 1.0/samples.size();
742 }
743 
744 #define MAXBIT 30
745 #define MAXDIM 6
746 void Random::
747 sobseq_(int *n, double x[])
748 {
749  int j,k,l;
750  unsigned long i,im,ipp;
751  static double fac;
752  static unsigned long in,ix[MAXDIM+1],*iu[MAXBIT+1];
753  static unsigned long mdeg[MAXDIM+1]={0,1,2,3,3,4,4};
754  static unsigned long ip[MAXDIM+1]={0,0,1,1,2,1,4};
755  static unsigned long iv[MAXDIM*MAXBIT+1]={
756  0,1,1,1,1,1,1,3,1,3,3,1,1,5,7,7,3,3,5,15,11,5,15,13,9};
757 
758  if (*n < 0) {
759  //cout << "initializing\n";
760  for (k=1;k<=MAXDIM;k++) ix[k]=0;
761  in=0;
762  if (iv[1] != 1) return;
763  fac=1.0/(1L << MAXBIT);
764  for (j=1,k=0;j<=MAXBIT;j++,k+=MAXDIM) iu[j] = &iv[k];
765  for (k=1;k<=MAXDIM;k++) {
766  for (j=1; j<=(int)mdeg[k]; j++)
767  iu[j][k] <<= (MAXBIT-j);
768 
769  for (j=mdeg[k]+1;j<=MAXBIT;j++) {
770  ipp=ip[k];
771  i=iu[j-mdeg[k]][k];
772  i ^= (i >> mdeg[k]);
773  for (l=mdeg[k]-1;l>=1;l--) {
774  if (ipp & 1) i ^= iu[j-l][k];
775  ipp >>= 1;
776  }
777  iu[j][k]=i;
778  }
779  }
780  } else {
781  im=in++;
782  for (j=1;j<=MAXBIT;j++) {
783  if (!(im & 1)) break;
784  im >>= 1;
785  }
786  if (j > MAXBIT) {
787  BIASERR("MAXBIT too small in sobseq");
788  BIASABORT;
789  }
790  im=(j-1)*MAXDIM;
791  for (k=1;k<=min(*n,MAXDIM);k++) {
792  ix[k] ^= iv[im+k];
793  //cout << "x["<<k-1<<"] = "<<x[k-1]<<endl;
794  x[k-1]=ix[k]*fac;
795  }
796  }
797 }
798 #undef MAXBIT
799 #undef MAXDIM
800 #undef NRANSI
801 
double GetNormalDistributedGSL(const double mean, const double sigma)
interface to GSL for comparison purposes
Definition: Random.cpp:184
Random()
initializes vars/seed with deterministic values, NOT calling reset
Definition: Random.cpp:69
double gasdevf_(long *idum)
Definition: Random.cpp:660
int GetQuasiUniformDistributed(const int dim, const Vector< double > &min, const Vector< double > &max, Vector< double > &res)
On subsequent calls returns a Sobol&#39; sequence of quasi random numbers &#39;uniformly distributed&#39; in a N-...
Definition: Random.cpp:300
Matrix< T > & newsize(Subscript M, Subscript N)
Definition: cmat.h:269
unsigned int Size() const
length of the vector
Definition: Vector.hh:143
double ran2_(long *idum)
Definition: Random.cpp:603
double GetUniformDistributed(const double min, const double max)
on succesive calls return uniform distributed random variable between min and max ...
Definition: Random.hh:84
int GetUniformDistributedInt(const int min, const int max)
get uniform distributed random variable including min/max
Definition: Random.cpp:139
double ran1c_(int *idum)
Definition: Random.cpp:554
double gasdev3_(int *idum)
Definition: Random.cpp:684
Vector< T > & newsize(Subscript N)
Definition: vec.h:220
void SetZero()
Sets all values to zero.
Definition: Matrix.hh:856
double gasdev_(int *idum)
produces normal distributed random variable with unit variance and zero mean
Definition: Random.cpp:637
double ran1_(int *idum)
produces uniform random variable between 0 and 1 exclusive
Definition: Random.cpp:503
long LongIdum_
Definition: Random.hh:188
int EigenvalueDecomposition(T &value1, Vector2< T > &vector1, T &value2, Vector2< T > &vector2) const
Eigenvalue decomposition.
Definition: Matrix2x2.cpp:131
unsigned int Seed_
Definition: Random.hh:186
void Reset()
calls srand() with a seed generated from system call time, also initializes some other variables ...
Definition: Random.cpp:113
Subscript size() const
Definition: cmat.h:212
int Uniform2Normal_(const double src, double &dst) const
transfroms a uniform disributed random variable into a normal distributed random variable ...
Definition: Random.cpp:456
double GetNormalDistributed(const double mean, const double sigma)
on succesive calls return normal distributed random variable with mean and standard deviation sigma ...
Definition: Random.hh:71
Subscript size() const
Definition: vec.h:262
void GetMeanAndCovariance(const std::vector< double > &samples, double &mean, double &covariance) const
check-function to compute mean and cov from samples
Definition: Random.cpp:706
void sobseq_(int *n, double x[])
Generates a sobol sequence of n dimensional &#39;quasi&#39; random vectors on succesive calls.
Definition: Random.cpp:747
int GetQuasiNormalDistributed(const int dim, Vector< double > &res)
On subsequent calls returns a Sobol&#39; sequence of quasi random numbers &#39;normal distributed&#39; with mean ...
Definition: Random.cpp:396