Random.cpp
Go to the documentation of this file.
00001 /*
00002  * Copyright (c) 2012 SCHUNK GmbH & Co. KG
00003  * Copyright (c) 2017 Fraunhofer Institute for Manufacturing Engineering and Automation (IPA)
00004  *
00005  * Licensed under the Apache License, Version 2.0 (the "License");
00006  * you may not use this file except in compliance with the License.
00007  * You may obtain a copy of the License at
00008  *
00009  *   http://www.apache.org/licenses/LICENSE-2.0
00010 
00011  * Unless required by applicable law or agreed to in writing, software
00012  * distributed under the License is distributed on an "AS IS" BASIS,
00013  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00014  * See the License for the specific language governing permissions and
00015  * limitations under the License.
00016  */
00017 
00018 #include "../Util/Random.h"
00019 
00020 // -------------------------------------------------------------------------- ;
00021 
00022 // \fdd{This function is an initialization entry point which should
00023 //              be invoked before util\_random() is called although an
00024 //              initializer value (FOR WIN32: derived from time()-function) is
00025 //              supplied automatically.
00026 //              With the same negative seed value a sequence of generated 
00027 //              random values can be reproduced at any time.}
00028 
00029 void util_seedRandom(long seedValA)
00030 {
00031 #if defined(_WIN32)
00032         fut_initRanG = seedValA;
00033 #else
00034         srand48(seedValA);
00035 #endif
00036 };
00037 
00038 // -------------------------------------------------------------------------- ;
00039 
00040 // \fdd{This function returns non-negative
00041 //              double-precision floating-point values uniformly distributed
00042 //              over the interval $[0.0, 1.0)$ .}
00043 
00044 double util_random()
00045 {
00046 #if defined(_WIN32)
00047 
00048         #define IA 16807
00049         #define IM 2147483647
00050         #define AM (1.0/IM)
00051         #define IQ 127773
00052         #define IR 2836
00053         #define NTAB 32
00054         #define NDIV (1+(IM-1)/NTAB)
00055         #define EPS 1.2e-7
00056         #define RNMX (1.0-EPS)
00057 
00058         int j;
00059         long k;
00060         static long iy=0;
00061         static long iv[NTAB];
00062         double temp;
00063 
00064         if (fut_initRanG <= 0 || !iy) {
00065                 if (-(fut_initRanG) < 1) fut_initRanG=1;
00066                 else fut_initRanG = -(fut_initRanG);
00067                 for (j=NTAB+7;j>=0;j--) {
00068                         k=(fut_initRanG)/IQ;
00069                         fut_initRanG=IA*(fut_initRanG-k*IQ)-IR*k;
00070                         if (fut_initRanG < 0) fut_initRanG += IM;
00071                         if (j < NTAB) iv[j] = fut_initRanG;
00072                 }
00073                 iy=iv[0];
00074         }
00075         k=(fut_initRanG)/IQ;
00076         fut_initRanG=IA*(fut_initRanG-k*IQ)-IR*k;
00077         if (fut_initRanG < 0) fut_initRanG += IM;
00078         j=iy/NDIV;
00079         iy=iv[j];
00080         iv[j] = fut_initRanG;
00081         if ((temp=AM*iy) > RNMX) return RNMX;
00082         else return temp;
00083         #undef IA
00084         #undef IM
00085         #undef AM
00086         #undef IQ
00087         #undef IR
00088         #undef NTAB
00089         #undef NDIV
00090         #undef EPS
00091         #undef RNMX
00092 
00093 #else
00094         return drand48();
00095 #endif
00096 };
00097 
00098 // -------------------------------------------------------------------------- ;
00099 
00100 // \fdd{This function returns
00101 //              double-precision floating-point values gaussian distributed
00102 //              with mean $0.0$ and variance $1.0$.  See \emph{Numerical Recipies} , 
00103 //       page 289 for further references. }
00104 double util_gaussRandom()
00105 {
00106         static int isetL = 0;
00107         static double gsetL;
00108         double facL, rsqL, v1L, v2L;
00109 
00110         if( isetL == 0 )
00111         {
00112                 do
00113                 {
00114                         v1L = 2.0 * util_random() - 1.0;
00115                         v2L = 2.0 * util_random() - 1.0;
00116                         rsqL = v1L * v1L + v2L * v2L;
00117                 }
00118                 while( rsqL >= 1.0 || rsqL == 0.0 );
00119                 facL = sqrt( -2.0 * log( rsqL ) / rsqL );
00120                 gsetL = v1L * facL;
00121                 isetL = 1;
00122                 return v2L * facL;
00123         }
00124         else
00125         {
00126                 isetL = 0;
00127                 return gsetL;
00128         }
00129 }
00130 
00131 // -------------------------------------------------------------------------- ;


schunk_libm5api
Author(s): Florian Weisshardt
autogenerated on Sat Jun 8 2019 20:25:13