Blame rng/mrg.c

Packit 67cb25
/* rng/mrg.c
Packit 67cb25
 * 
Packit 67cb25
 * Copyright (C) 1996, 1997, 1998, 1999, 2000, 2007 James Theiler, Brian Gough
Packit 67cb25
 * 
Packit 67cb25
 * This program is free software; you can redistribute it and/or modify
Packit 67cb25
 * it under the terms of the GNU General Public License as published by
Packit 67cb25
 * the Free Software Foundation; either version 3 of the License, or (at
Packit 67cb25
 * your option) any later version.
Packit 67cb25
 * 
Packit 67cb25
 * This program is distributed in the hope that it will be useful, but
Packit 67cb25
 * WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 67cb25
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit 67cb25
 * General Public License for more details.
Packit 67cb25
 * 
Packit 67cb25
 * You should have received a copy of the GNU General Public License
Packit 67cb25
 * along with this program; if not, write to the Free Software
Packit 67cb25
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
Packit 67cb25
 */
Packit 67cb25
Packit 67cb25
#include <config.h>
Packit 67cb25
#include <stdlib.h>
Packit 67cb25
#include <gsl/gsl_rng.h>
Packit 67cb25
Packit 67cb25
/* This is a fifth-order multiple recursive generator. The sequence is,
Packit 67cb25
Packit 67cb25
   x_n = (a_1 x_{n-1} + a_5 x_{n-5}) mod m
Packit 67cb25
Packit 67cb25
   with a_1 = 107374182, a_2 = a_3 = a_4 = 0, a_5 = 104480 and m = 2^31-1.
Packit 67cb25
Packit 67cb25
   We initialize the generator with x_n = s_n MOD m for n = 1..5,
Packit 67cb25
   where s_n = (69069 * s_{n-1}) mod 2^32, and s_0 = s is the
Packit 67cb25
   user-supplied seed.
Packit 67cb25
Packit 67cb25
   NOTE: According to the paper the seeds must lie in the range [0,
Packit 67cb25
   2^31 - 2] with at least one non-zero value -- our seeding procedure
Packit 67cb25
   satisfies these constraints.
Packit 67cb25
Packit 67cb25
   We then use 6 iterations of the generator to "warm up" the internal
Packit 67cb25
   state.
Packit 67cb25
Packit 67cb25
   With this initialization procedure the theoretical value of
Packit 67cb25
   z_{10006} is 2064828650 for s = 1. The subscript 10006 means (1)
Packit 67cb25
   seed the generator with s = 1, (2) do the 6 warm-up iterations
Packit 67cb25
   that are part of the seeding process, (3) then do 10000 actual
Packit 67cb25
   iterations.
Packit 67cb25
Packit 67cb25
   The period of this generator is about 2^155.
Packit 67cb25
Packit 67cb25
   From: P. L'Ecuyer, F. Blouin, and R. Coutre, "A search for good
Packit 67cb25
   multiple recursive random number generators", ACM Transactions on
Packit 67cb25
   Modeling and Computer Simulation 3, 87-98 (1993). */
Packit 67cb25
Packit 67cb25
static inline unsigned long int mrg_get (void *vstate);
Packit 67cb25
static double mrg_get_double (void *vstate);
Packit 67cb25
static void mrg_set (void *state, unsigned long int s);
Packit 67cb25
Packit 67cb25
static const long int m = 2147483647;
Packit 67cb25
static const long int a1 = 107374182, q1 = 20, r1 = 7;
Packit 67cb25
static const long int a5 = 104480, q5 = 20554, r5 = 1727;
Packit 67cb25
Packit 67cb25
typedef struct
Packit 67cb25
  {
Packit 67cb25
    long int x1, x2, x3, x4, x5;
Packit 67cb25
  }
Packit 67cb25
mrg_state_t;
Packit 67cb25
Packit 67cb25
static inline unsigned long int
Packit 67cb25
mrg_get (void *vstate)
Packit 67cb25
{
Packit 67cb25
  mrg_state_t *state = (mrg_state_t *) vstate;
Packit 67cb25
Packit 67cb25
  long int p1, h1, p5, h5;
Packit 67cb25
Packit 67cb25
  h5 = state->x5 / q5;
Packit 67cb25
  p5 = a5 * (state->x5 - h5 * q5) - h5 * r5;
Packit 67cb25
  if (p5 > 0)
Packit 67cb25
    p5 -= m;
Packit 67cb25
Packit 67cb25
  h1 = state->x1 / q1;
Packit 67cb25
  p1 = a1 * (state->x1 - h1 * q1) - h1 * r1;
Packit 67cb25
  if (p1 < 0)
Packit 67cb25
    p1 += m;
Packit 67cb25
Packit 67cb25
  state->x5 = state->x4;
Packit 67cb25
  state->x4 = state->x3;
Packit 67cb25
  state->x3 = state->x2;
Packit 67cb25
  state->x2 = state->x1;
Packit 67cb25
Packit 67cb25
  state->x1 = p1 + p5;
Packit 67cb25
Packit 67cb25
  if (state->x1 < 0)
Packit 67cb25
    state->x1 += m;
Packit 67cb25
Packit 67cb25
  return state->x1;
Packit 67cb25
}
Packit 67cb25
Packit 67cb25
static double
Packit 67cb25
mrg_get_double (void *vstate)
Packit 67cb25
{
Packit 67cb25
  return mrg_get (vstate) / 2147483647.0 ;
Packit 67cb25
}
Packit 67cb25
Packit 67cb25
Packit 67cb25
static void
Packit 67cb25
mrg_set (void *vstate, unsigned long int s)
Packit 67cb25
{
Packit 67cb25
  /* An entirely adhoc way of seeding! This does **not** come from
Packit 67cb25
     L'Ecuyer et al */
Packit 67cb25
Packit 67cb25
  mrg_state_t *state = (mrg_state_t *) vstate;
Packit 67cb25
Packit 67cb25
  if (s == 0)
Packit 67cb25
    s = 1;      /* default seed is 1 */
Packit 67cb25
Packit 67cb25
#define LCG(n) ((69069 * n) & 0xffffffffUL)
Packit 67cb25
  s = LCG (s);
Packit 67cb25
  state->x1 = s % m;
Packit 67cb25
  s = LCG (s);
Packit 67cb25
  state->x2 = s % m;
Packit 67cb25
  s = LCG (s);
Packit 67cb25
  state->x3 = s % m;
Packit 67cb25
  s = LCG (s);
Packit 67cb25
  state->x4 = s % m;
Packit 67cb25
  s = LCG (s);
Packit 67cb25
  state->x5 = s % m;
Packit 67cb25
Packit 67cb25
  /* "warm it up" with at least 5 calls to go through
Packit 67cb25
     all the x values */
Packit 67cb25
Packit 67cb25
  mrg_get (state);
Packit 67cb25
  mrg_get (state);
Packit 67cb25
  mrg_get (state);
Packit 67cb25
  mrg_get (state);
Packit 67cb25
  mrg_get (state);
Packit 67cb25
  mrg_get (state);
Packit 67cb25
Packit 67cb25
  return;
Packit 67cb25
}
Packit 67cb25
Packit 67cb25
static const gsl_rng_type mrg_type =
Packit 67cb25
{"mrg",                         /* name */
Packit 67cb25
 2147483646,                    /* RAND_MAX */
Packit 67cb25
 0,                             /* RAND_MIN */
Packit 67cb25
 sizeof (mrg_state_t),
Packit 67cb25
 &mrg_set,
Packit 67cb25
 &mrg_get,
Packit 67cb25
 &mrg_get_double};
Packit 67cb25
Packit 67cb25
const gsl_rng_type *gsl_rng_mrg = &mrg_type;