Eigene Dateien/FlowVis/src/mother.cpp

Go to the documentation of this file.
00001 /************************** MOTHER.CPP ****************** AgF 2007-08-01 *
00002 *  'Mother-of-All' random number generator                               *
00003 *                                                                        *
00004 *  This is a multiply-with-carry type of random number generator         *
00005 *  invented by George Marsaglia.  The algorithm is:                      *
00006 *  S = 2111111111*X[n-4] + 1492*X[n-3] + 1776*X[n-2] + 5115*X[n-1] + C   *
00007 *  X[n] = S modulo 2^32                                                  *
00008 *  C = floor(S / 2^32)                                                   *
00009 *                                                                        *
00010 *  Note:                                                                 *
00011 *  This implementation uses 64-bit integers for intermediate             *
00012 *  calculations. Works only on compilers that support 64-bit integers.   *
00013 *                                                                        *
00014 * © 1999 - 2007 A. Fog.                                                  *
00015 * GNU General Public License www.gnu.org/copyleft/gpl.html               *
00016 *************************************************************************/
00017 
00018 #include "randomc.h"
00019 
00020 
00021 // Output random bits
00022 uint32 CRandomMother::BRandom() {
00023   uint64 sum;
00024   sum = (uint64)2111111111ul * (uint64)x[3] +
00025      (uint64)1492 * (uint64)(x[2]) +
00026      (uint64)1776 * (uint64)(x[1]) +
00027      (uint64)5115 * (uint64)(x[0]) +
00028      (uint64)x[4];
00029   x[3] = x[2];  x[2] = x[1];  x[1] = x[0];
00030   x[4] = uint32(sum >> 32);            // Carry
00031   x[0] = uint32(sum);                  // Low 32 bits of sum
00032   return x[0];
00033 } 
00034 
00035 
00036 // returns a random number between 0 and 1:
00037 double CRandomMother::Random() {
00038    return (double)BRandom() * (1./(65536.*65536.));
00039 }
00040 
00041 
00042 // returns integer random number in desired interval:
00043 int CRandomMother::IRandom(int min, int max) {
00044    // Output random integer in the interval min <= x <= max
00045    // Relative error on frequencies < 2^-32
00046    if (max <= min) {
00047       if (max == min) return min; else return 0x80000000;
00048    }
00049    // Multiply interval with random and truncate
00050    int r = int((max - min + 1) * Random()) + min; 
00051    if (r > max) r = max;
00052    return r;
00053 }
00054 
00055 
00056 // this function initializes the random number generator:
00057 void CRandomMother::RandomInit (uint32 seed) {
00058   int i;
00059   uint32 s = seed;
00060   // make random numbers and put them into the buffer
00061   for (i = 0; i < 5; i++) {
00062     s = s * 29943829 - 1;
00063     x[i] = s;
00064   }
00065   // randomize some more
00066   for (i=0; i<19; i++) BRandom();
00067 }

Generated on Mon Jan 21 01:15:15 2008 for FlowVis by  doxygen 1.5.4