single-rnd.c

     
   1  //! @file single-rnd.c
   2  //! @author J. Marcel van der Veer
   3  
   4  //! @section Copyright
   5  //!
   6  //! This file is part of Algol68G - an Algol 68 compiler-interpreter.
   7  //! Copyright 2001-2024 J. Marcel van der Veer [algol68g@xs4all.nl].
   8  
   9  //! @section License
  10  //!
  11  //! This program is free software; you can redistribute it and/or modify it 
  12  //! under the terms of the GNU General Public License as published by the 
  13  //! Free Software Foundation; either version 3 of the License, or 
  14  //! (at your option) any later version.
  15  //!
  16  //! This program is distributed in the hope that it will be useful, but 
  17  //! WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 
  18  //! or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for 
  19  //! more details. You should have received a copy of the GNU General Public 
  20  //! License along with this program. If not, see [http://www.gnu.org/licenses/].
  21  
  22  //! @section Synopsis
  23  //!
  24  //! REAL pseudo-random number generator.
  25  
  26  #include "a68g.h"
  27  
  28  // Next part is a "stand-alone" version of GNU Scientific Library (GSL)
  29  // random number generator "taus113", based on GSL file "rng/taus113.c".
  30  // 
  31  // Copyright (C) 2002 Atakan Gurkan
  32  // Based on the file taus.c which has the notice
  33  // Copyright (C) 1996, 1997, 1998, 1999, 2000, 2007 James Theiler, Brian Gough
  34  // 
  35  // This program is free software; you can redistribute it and/or modify
  36  // it under the terms of the GNU General Public License as published by
  37  // the Free Software Foundation; either version 3 of the License, or (at
  38  // your option) any later version.
  39  // 
  40  // This program is distributed in the hope that it will be useful, but
  41  // WITHOUT ANY WARRANTY; without even the implied warranty of
  42  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  43  // General Public License for more details.
  44  // 
  45  // You should have received a copy of the GNU General Public License
  46  // along with this program; if not, write to the Free Software
  47  // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  48  // This is a maximally equidistributed combined, collision free 
  49  // Tausworthe generator, with a period ~2^{113}. The sequence is,
  50  //   x_n = (z1_n ^ z2_n ^ z3_n ^ z4_n)  
  51  //   b = (((z1_n <<  6) ^ z1_n) >> 13)
  52  //   z1_{n+1} = (((z1_n & 4294967294) << 18) ^ b)
  53  //   b = (((z2_n <<  2) ^ z2_n) >> 27)
  54  //   z2_{n+1} = (((z2_n & 4294967288) <<  2) ^ b)
  55  //   b = (((z3_n << 13) ^ z3_n) >> 21)
  56  //   z3_{n+1} = (((z3_n & 4294967280) <<  7) ^ b)
  57  //   b = (((z4_n <<  3)  ^ z4_n) >> 12)
  58  //   z4_{n+1} = (((z4_n & 4294967168) << 13) ^ b)
  59  // computed modulo 2^32. In the formulas above '^' means exclusive-or 
  60  // (C-notation), not exponentiation. 
  61  // The algorithm is for 32-bit integers, hence a bitmask is used to clear 
  62  // all but least significant 32 bits, after left shifts, to make the code 
  63  // work on architectures where integers are 64-bit.
  64  // The generator is initialized with 
  65  // z{i+1} = (69069 * zi) MOD 2^32 where z0 is the seed provided
  66  // During initialization a check is done to make sure that the initial seeds 
  67  // have a required number of their most significant bits set.
  68  // After this, the state is passed through the RNG 10 times to ensure the
  69  // state satisfies a recurrence relation.
  70  // References:
  71  //   P. L'Ecuyer, "Tables of Maximally-Equidistributed Combined LFSR Generators",
  72  //   Mathematics of Computation, 68, 225 (1999), 261--269.
  73  //     http://www.iro.umontreal.ca/~lecuyer/myftp/papers/tausme2.ps
  74  //   P. L'Ecuyer, "Maximally Equidistributed Combined Tausworthe Generators", 
  75  //   Mathematics of Computation, 65, 213 (1996), 203--213.
  76  //     http://www.iro.umontreal.ca/~lecuyer/myftp/papers/tausme.ps
  77  //   the online version of the latter contains corrections to the print version.
  78  
  79  #define LCG(n) ((69069UL * n) & 0xffffffffUL)
  80  #define MASK 0xffffffffUL
  81  
  82  unt taus113_get (void *vstate);
  83  REAL_T taus113_get_double (void *vstate);
  84  void taus113_set (void *state, unt long int s);
  85  
  86  typedef struct
  87  {
  88    unt long int z1, z2, z3, z4;
  89  }
  90  taus113_state_t;
  91  
  92  static taus113_state_t rng_state;
  93  
  94  unt taus113_get (void *vstate)
  95  {
  96    taus113_state_t *state = (taus113_state_t *) vstate;
  97    unt long b1 = ((((state->z1 << 6UL) & MASK) ^ state->z1) >> 13UL);
  98    state->z1 = ((((state->z1 & 4294967294UL) << 18UL) & MASK) ^ b1);
  99    unt long b2 = ((((state->z2 << 2UL) & MASK) ^ state->z2) >> 27UL);
 100    state->z2 = ((((state->z2 & 4294967288UL) << 2UL) & MASK) ^ b2);
 101    unt long b3 = ((((state->z3 << 13UL) & MASK) ^ state->z3) >> 21UL);
 102    state->z3 = ((((state->z3 & 4294967280UL) << 7UL) & MASK) ^ b3);
 103    unt long b4 = ((((state->z4 << 3UL) & MASK) ^ state->z4) >> 12UL);
 104    state->z4 = ((((state->z4 & 4294967168UL) << 13UL) & MASK) ^ b4);
 105    return (state->z1 ^ state->z2 ^ state->z3 ^ state->z4);
 106  }
 107  
 108  REAL_T taus113_get_double (void *vstate)
 109  {
 110    return taus113_get (vstate) / 4294967296.0;
 111  }
 112  
 113  void taus113_set (void *vstate, unt long int s)
 114  {
 115    taus113_state_t *state = (taus113_state_t *) vstate;
 116    if (!s) {
 117      s = 1UL; // default seed is 1
 118    }
 119    state->z1 = LCG (s);
 120    if (state->z1 < 2UL) {
 121      state->z1 += 2UL;
 122    }
 123    state->z2 = LCG (state->z1);
 124    if (state->z2 < 8UL) {
 125      state->z2 += 8UL;
 126    }
 127    state->z3 = LCG (state->z2);
 128    if (state->z3 < 16UL) {
 129      state->z3 += 16UL;
 130    }
 131    state->z4 = LCG (state->z3);
 132    if (state->z4 < 128UL) {
 133      state->z4 += 128UL;
 134    }
 135  // Calling RNG ten times to satify recurrence condition
 136    taus113_get (state);
 137    taus113_get (state);
 138    taus113_get (state);
 139    taus113_get (state);
 140    taus113_get (state);
 141    taus113_get (state);
 142    taus113_get (state);
 143    taus113_get (state);
 144    taus113_get (state);
 145    taus113_get (state);
 146    return;
 147  }
 148  
 149  // Initialise rng.
 150  
 151  void init_rng (unt u)
 152  {
 153    taus113_set (&rng_state, u);
 154  }
 155  
 156  // A68G rng in R mathlib style.
 157  
 158  REAL_T a68_unif_rand (void)
 159  {
 160  // In [0, 1>
 161    return taus113_get_double (&rng_state);
 162  }
 163  
 164  REAL_T a68_gauss_rand (void) 
 165  {
 166  // Marsaglia polar method instead of Box-Muller transform.
 167    REAL_T s;
 168    do {
 169       REAL_T v1 = 2 * a68_unif_rand () - 1;
 170       REAL_T v2 = 2 * a68_unif_rand () - 1;
 171       s = v1 * v1 + v2 * v2;
 172    } while (s >= 1 || s == 0); // A fraction (1-pi/4) is rejected.
 173    return sqrt (-2 * log (s) / s);
 174  }
 175  
 176  static char *state_file = ".Random.seed";
 177  
 178  void GetRNGstate (void)
 179  {
 180    INT_T fd = open (state_file, A68_READ_ACCESS);
 181    if (fd != -1) {
 182      ASSERT (read (fd, &rng_state, sizeof (taus113_state_t)) != -1);
 183      close (fd);
 184    }
 185  }
 186  
 187  void PutRNGstate (void)
 188  {
 189    INT_T fd = open (state_file, A68_WRITE_ACCESS, A68_PROTECTION);
 190    if (fd != -1) {
 191      ASSERT (write (fd, &rng_state, sizeof (taus113_state_t)) != -1);
 192      close (fd);
 193    }
 194  }