ViSP  2.10.0
vpNoise.cpp
1 /****************************************************************************
2  *
3  * $Id: vpNoise.cpp 4975 2014-11-17 15:59:42Z fspindle $
4  *
5  * This file is part of the ViSP software.
6  * Copyright (C) 2005 - 2014 by INRIA. All rights reserved.
7  *
8  * This software is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License
10  * ("GPL") version 2 as published by the Free Software Foundation.
11  * See the file LICENSE.txt at the root directory of this source
12  * distribution for additional information about the GNU GPL.
13  *
14  * For using ViSP with software that can not be combined with the GNU
15  * GPL, please contact INRIA about acquiring a ViSP Professional
16  * Edition License.
17  *
18  * See http://www.irisa.fr/lagadic/visp/visp.html for more information.
19  *
20  * This software was developed at:
21  * INRIA Rennes - Bretagne Atlantique
22  * Campus Universitaire de Beaulieu
23  * 35042 Rennes Cedex
24  * France
25  * http://www.irisa.fr/lagadic
26  *
27  * If you have questions regarding the use of this file, please contact
28  * INRIA at visp@inria.fr
29  *
30  * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
31  * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
32  *
33  *
34  * Description:
35  * Generation of random number with uniform and normal probability density.
36  *
37  * Authors:
38  * Eric Marchand
39  *
40  *****************************************************************************/
41 
42 #include <visp/vpNoise.h>
43 #include <math.h>
44 
59 inline void
60 vpUniRand::draw0()
61 //minimal standard
62 //Park and Miller random number generator
63 {
64  /*unsigned*/ long k= x/q;//temp value for computing without overflow
65  x = a*(x-k*q)-k*r;
66  if (x < 0) x += m; //compute x without overflow
67 }
68 
76 double
78 {
79  const long ntab = 33; //we work on a 32 elements array.
80  //the 33rd one is actually the first value of y.
81  const long modulo = ntab-2;
82 
83  static long y = 0;
84  static long T[ntab];
85 
86  long j; //index of T
87 
88  //step 0
89  if (!y) { //first time
90  for(j = 0; j < ntab; j++) {
91  draw0();
92  T[j]=x;
93  } //compute table T
94  y=T[ntab-1];
95  }
96 
97  //step 1
98  j = y & modulo; //compute modulo ntab+1 (the first element is the 0th)
99 
100  //step 3
101  y=T[j];
102  double ans = (double)y/normalizer;
103 
104  //step 4
105  //generate x(k+i) and set y=x(k+i)
106  draw0();
107 
108  //refresh T[j];
109  T[j]=x;
110 
111  return ans;
112 }
113 
121 double
122 vpGaussRand::gaussianDraw()
123 {
124  double v1, v2, rsq;
125  static bool AlreadyDone = false;
126  static double x2;
127 
128  if (AlreadyDone) {
129  AlreadyDone=false;
130  return x2;
131  }
132 
133  else {
134 
135  do {
136  v1=2*draw1()-1;
137  v2=2*draw1()-1;
138  rsq=v1*v1+v2*v2;
139  } while (rsq >= 1);
140 
141  double fac=sqrt(-2*log(rsq)/rsq);
142  x2=v2*fac;
143  AlreadyDone=true;
144  return v1*fac;
145  }
146 }
147 
double draw1()
Definition: vpNoise.cpp:77
long x
Definition: vpNoise.h:79