single-rnd.c

You can download the current version of Algol 68 Genie and its documentation here.

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