modules/up/src/Core/gnu/MLCG.cc

/* [<][>]
[^][v][top][bottom][index][help] */

FUNCTIONS

This source file includes following functions.
  1. MLCG
  2. reset
  3. asLong

   1 // This may look like C code, but it is really -*- C++ -*-
   2 /* 
   3 Copyright (C) 1989 Free Software Foundation
   4 
   5 This file is part of the GNU C++ Library.  This library is free
   6 software; you can redistribute it and/or modify it under the terms of
   7 the GNU Library General Public License as published by the Free
   8 Software Foundation; either version 2 of the License, or (at your
   9 option) any later version.  This library is distributed in the hope
  10 that it will be useful, but WITHOUT ANY WARRANTY; without even the
  11 implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
  12 PURPOSE.  See the GNU Library General Public License for more details.
  13 You should have received a copy of the GNU Library General Public
  14 License along with this library; if not, write to the Free Software
  15 Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  16 */
  17 #ifdef __GNUG__
  18 #pragma implementation
  19 #endif
  20 #include "gnu/MLCG.h"
  21 //
  22 //      SEED_TABLE_SIZE must be a power of 2
  23 //
  24 
  25 
  26 #define SEED_TABLE_SIZE 32
  27 
  28 static _G_int32_t seedTable[SEED_TABLE_SIZE] = {
  29 0xbdcc47e5, 0x54aea45d, 0xec0df859, 0xda84637b,
  30 0xc8c6cb4f, 0x35574b01, 0x28260b7d, 0x0d07fdbf,
  31 0x9faaeeb0, 0x613dd169, 0x5ce2d818, 0x85b9e706,
  32 0xab2469db, 0xda02b0dc, 0x45c60d6e, 0xffe49d10,
  33 0x7224fea3, 0xf9684fc9, 0xfc7ee074, 0x326ce92a,
  34 0x366d13b5, 0x17aaa731, 0xeb83a675, 0x7781cb32,
  35 0x4ec7c92d, 0x7f187521, 0x2cf346b4, 0xad13310f,
  36 0xb89cff2b, 0x12164de1, 0xa865168d, 0x32b56cdf
  37 };
  38 
  39 MLCG::MLCG(_G_int32_t seed1, _G_int32_t seed2)
     /* [<][>][^][v][top][bottom][index][help] */
  40 {
  41     initialSeedOne = seed1;
  42     initialSeedTwo = seed2;
  43     reset();
  44 }
  45 
  46 void
  47 MLCG::reset()
     /* [<][>][^][v][top][bottom][index][help] */
  48 {
  49     _G_int32_t seed1 = initialSeedOne;
  50     _G_int32_t seed2 = initialSeedTwo;
  51 
  52     //
  53     //  Most people pick stupid seed numbers that do not have enough
  54     //  bits. In this case, if they pick a small seed number, we
  55     //  map that to a specific seed.
  56     //
  57     if (seed1 < 0) {
  58         seed1 = (seed1 + 2147483561);
  59         seed1 = (seed1 < 0) ? -seed1 : seed1;
  60     }
  61 
  62     if (seed2 < 0) {
  63         seed2 = (seed2 + 2147483561);
  64         seed2 = (seed2 < 0) ? -seed2 : seed2;
  65     }
  66 
  67     if (seed1 > -1 && seed1 < SEED_TABLE_SIZE) {
  68         seedOne = seedTable[seed1];
  69     } else {
  70         seedOne = seed1 ^ seedTable[seed1 & (SEED_TABLE_SIZE-1)];
  71     }
  72 
  73     if (seed2 > -1 && seed2 < SEED_TABLE_SIZE) {
  74         seedTwo = seedTable[seed2];
  75     } else {
  76         seedTwo = seed2 ^ seedTable[ seed2 & (SEED_TABLE_SIZE-1) ];
  77     }
  78     seedOne = (seedOne % 2147483561) + 1;
  79     seedTwo = (seedTwo % 2147483397) + 1;
  80 }
  81 
  82 _G_uint32_t MLCG::asLong()
     /* [<][>][^][v][top][bottom][index][help] */
  83 {
  84     _G_int32_t k = seedOne % 53668;
  85 
  86     seedOne = 40014 * (seedOne-k * 53668) - k * 12211;
  87     if (seedOne < 0) {
  88         seedOne += 2147483563;
  89     }
  90 
  91     k = seedTwo % 52774;
  92     seedTwo = 40692 * (seedTwo - k * 52774) - k * 3791;
  93     if (seedTwo < 0) {
  94         seedTwo += 2147483399;
  95     }
  96 
  97     _G_int32_t z = seedOne - seedTwo;
  98     if (z < 1) {
  99         z += 2147483562;
 100     }
 101     return( (unsigned long) z);
 102 }
 103 

/* [<][>][^][v][top][bottom][index][help] */