Random.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2012 SCHUNK GmbH & Co. KG
3  * Copyright (c) 2017 Fraunhofer Institute for Manufacturing Engineering and Automation (IPA)
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10 
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17 
18 #include "../Util/Random.h"
19 
20 // -------------------------------------------------------------------------- ;
21 
22 // \fdd{This function is an initialization entry point which should
23 // be invoked before util\_random() is called although an
24 // initializer value (FOR WIN32: derived from time()-function) is
25 // supplied automatically.
26 // With the same negative seed value a sequence of generated
27 // random values can be reproduced at any time.}
28 
29 void util_seedRandom(long seedValA)
30 {
31 #if defined(_WIN32)
32  fut_initRanG = seedValA;
33 #else
34  srand48(seedValA);
35 #endif
36 };
37 
38 // -------------------------------------------------------------------------- ;
39 
40 // \fdd{This function returns non-negative
41 // double-precision floating-point values uniformly distributed
42 // over the interval $[0.0, 1.0)$ .}
43 
44 double util_random()
45 {
46 #if defined(_WIN32)
47 
48  #define IA 16807
49  #define IM 2147483647
50  #define AM (1.0/IM)
51  #define IQ 127773
52  #define IR 2836
53  #define NTAB 32
54  #define NDIV (1+(IM-1)/NTAB)
55  #define EPS 1.2e-7
56  #define RNMX (1.0-EPS)
57 
58  int j;
59  long k;
60  static long iy=0;
61  static long iv[NTAB];
62  double temp;
63 
64  if (fut_initRanG <= 0 || !iy) {
65  if (-(fut_initRanG) < 1) fut_initRanG=1;
66  else fut_initRanG = -(fut_initRanG);
67  for (j=NTAB+7;j>=0;j--) {
68  k=(fut_initRanG)/IQ;
69  fut_initRanG=IA*(fut_initRanG-k*IQ)-IR*k;
70  if (fut_initRanG < 0) fut_initRanG += IM;
71  if (j < NTAB) iv[j] = fut_initRanG;
72  }
73  iy=iv[0];
74  }
75  k=(fut_initRanG)/IQ;
76  fut_initRanG=IA*(fut_initRanG-k*IQ)-IR*k;
77  if (fut_initRanG < 0) fut_initRanG += IM;
78  j=iy/NDIV;
79  iy=iv[j];
80  iv[j] = fut_initRanG;
81  if ((temp=AM*iy) > RNMX) return RNMX;
82  else return temp;
83  #undef IA
84  #undef IM
85  #undef AM
86  #undef IQ
87  #undef IR
88  #undef NTAB
89  #undef NDIV
90  #undef EPS
91  #undef RNMX
92 
93 #else
94  return drand48();
95 #endif
96 };
97 
98 // -------------------------------------------------------------------------- ;
99 
100 // \fdd{This function returns
101 // double-precision floating-point values gaussian distributed
102 // with mean $0.0$ and variance $1.0$. See \emph{Numerical Recipies} ,
103 // page 289 for further references. }
105 {
106  static int isetL = 0;
107  static double gsetL;
108  double facL, rsqL, v1L, v2L;
109 
110  if( isetL == 0 )
111  {
112  do
113  {
114  v1L = 2.0 * util_random() - 1.0;
115  v2L = 2.0 * util_random() - 1.0;
116  rsqL = v1L * v1L + v2L * v2L;
117  }
118  while( rsqL >= 1.0 || rsqL == 0.0 );
119  facL = sqrt( -2.0 * log( rsqL ) / rsqL );
120  gsetL = v1L * facL;
121  isetL = 1;
122  return v2L * facL;
123  }
124  else
125  {
126  isetL = 0;
127  return gsetL;
128  }
129 }
130 
131 // -------------------------------------------------------------------------- ;
void srand48(long seedval)
double drand48(void)
void util_seedRandom(long seedValA)
Definition: Random.cpp:29
double util_random()
Definition: Random.cpp:44
double util_gaussRandom()
Definition: Random.cpp:104


schunk_libm5api
Author(s): Florian Weisshardt
autogenerated on Mon Nov 25 2019 03:48:19