Random.cpp
Go to the documentation of this file.
00001 
00002 /******************************************************************************
00003  * 
00004  * Copyright (c) 2012 
00005  * 
00006  * SCHUNK GmbH & Co. KG
00007  *  
00008  * ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 
00009  * 
00010  * Project name: Drivers for "Amtec M5 Protocol" Electronics V4
00011  *                                                                        
00012  * ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 
00013  * 
00014  * Email:robotics@schunk.com
00015  * 
00016  * ToDo: 
00017  * 
00018  * ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 
00019  * 
00020  * Redistribution and use in source and binary forms, with or without 
00021  * modification, are permitted provided that the following conditions are met: 
00022  * 
00023  *  * Redistributions of source code must retain the above copyright 
00024  *    notice, this list of conditions and the following disclaimer. 
00025  *  * Redistributions in binary form must reproduce the above copyright 
00026  *    notice, this list of conditions and the following disclaimer in the 
00027  *    documentation and/or other materials provided with the distribution. 
00028  *  * Neither the name of SCHUNK GmbH & Co. KG nor the names of its 
00029  *    contributors may be used to endorse or promote products derived from 
00030  *    this software without specific prior written permission. 
00031  * 
00032  * This program is free software: you can redistribute it and/or modify 
00033  * it under the terms of the GNU Lesser General Public License LGPL as 
00034  * published by the Free Software Foundation, either version 3 of the 
00035  * License, or (at your option) any later version. 
00036  * 
00037  * This program is distributed in the hope that it will be useful, 
00038  * but WITHOUT ANY WARRANTY; without even the implied warranty of 
00039  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
00040  * GNU Lesser General Public License LGPL for more details. 
00041  * 
00042  * You should have received a copy of the GNU Lesser General Public 
00043  * License LGPL along with this program. 
00044  * If not, see <http://www.gnu.org/licenses/>.
00045  * 
00046  ******************************************************************************/
00047 
00048 
00049 #include "../Util/Random.h"
00050 
00051 // -------------------------------------------------------------------------- ;
00052 
00053 // \fdd{This function is an initialization entry point which should
00054 //              be invoked before util\_random() is called although an
00055 //              initializer value (FOR WIN32: derived from time()-function) is
00056 //              supplied automatically.
00057 //              With the same negative seed value a sequence of generated 
00058 //              random values can be reproduced at any time.}
00059 
00060 void util_seedRandom(long seedValA)
00061 {
00062 #if defined(_WIN32)
00063         fut_initRanG = seedValA;
00064 #else
00065         srand48(seedValA);
00066 #endif
00067 };
00068 
00069 // -------------------------------------------------------------------------- ;
00070 
00071 // \fdd{This function returns non-negative
00072 //              double-precision floating-point values uniformly distributed
00073 //              over the interval $[0.0, 1.0)$ .}
00074 
00075 double util_random()
00076 {
00077 #if defined(_WIN32)
00078 
00079         #define IA 16807
00080         #define IM 2147483647
00081         #define AM (1.0/IM)
00082         #define IQ 127773
00083         #define IR 2836
00084         #define NTAB 32
00085         #define NDIV (1+(IM-1)/NTAB)
00086         #define EPS 1.2e-7
00087         #define RNMX (1.0-EPS)
00088 
00089         int j;
00090         long k;
00091         static long iy=0;
00092         static long iv[NTAB];
00093         double temp;
00094 
00095         if (fut_initRanG <= 0 || !iy) {
00096                 if (-(fut_initRanG) < 1) fut_initRanG=1;
00097                 else fut_initRanG = -(fut_initRanG);
00098                 for (j=NTAB+7;j>=0;j--) {
00099                         k=(fut_initRanG)/IQ;
00100                         fut_initRanG=IA*(fut_initRanG-k*IQ)-IR*k;
00101                         if (fut_initRanG < 0) fut_initRanG += IM;
00102                         if (j < NTAB) iv[j] = fut_initRanG;
00103                 }
00104                 iy=iv[0];
00105         }
00106         k=(fut_initRanG)/IQ;
00107         fut_initRanG=IA*(fut_initRanG-k*IQ)-IR*k;
00108         if (fut_initRanG < 0) fut_initRanG += IM;
00109         j=iy/NDIV;
00110         iy=iv[j];
00111         iv[j] = fut_initRanG;
00112         if ((temp=AM*iy) > RNMX) return RNMX;
00113         else return temp;
00114         #undef IA
00115         #undef IM
00116         #undef AM
00117         #undef IQ
00118         #undef IR
00119         #undef NTAB
00120         #undef NDIV
00121         #undef EPS
00122         #undef RNMX
00123 
00124 #else
00125         return drand48();
00126 #endif
00127 };
00128 
00129 // -------------------------------------------------------------------------- ;
00130 
00131 // \fdd{This function returns
00132 //              double-precision floating-point values gaussian distributed
00133 //              with mean $0.0$ and variance $1.0$.  See \emph{Numerical Recipies} , 
00134 //       page 289 for further references. }
00135 double util_gaussRandom()
00136 {
00137         static int isetL = 0;
00138         static double gsetL;
00139         double facL, rsqL, v1L, v2L;
00140 
00141         if( isetL == 0 )
00142         {
00143                 do
00144                 {
00145                         v1L = 2.0 * util_random() - 1.0;
00146                         v2L = 2.0 * util_random() - 1.0;
00147                         rsqL = v1L * v1L + v2L * v2L;
00148                 }
00149                 while( rsqL >= 1.0 || rsqL == 0.0 );
00150                 facL = sqrt( -2.0 * log( rsqL ) / rsqL );
00151                 gsetL = v1L * facL;
00152                 isetL = 1;
00153                 return v2L * facL;
00154         }
00155         else
00156         {
00157                 isetL = 0;
00158                 return gsetL;
00159         }
00160 }
00161 
00162 // -------------------------------------------------------------------------- ;


schunk_libm5api
Author(s): Florian Weisshardt
autogenerated on Thu Aug 27 2015 15:06:52