Blame rng/rand48.c

Packit 67cb25
/* rng/rand48.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 <math.h>
Packit 67cb25
#include <stdlib.h>
Packit 67cb25
#include <gsl/gsl_sys.h>
Packit 67cb25
#include <gsl/gsl_rng.h>
Packit 67cb25
Packit 67cb25
/* This is the Unix rand48() generator. The generator returns the
Packit 67cb25
   upper 32 bits from each term of the sequence,
Packit 67cb25
Packit 67cb25
   x_{n+1} = (a x_n + c) mod m 
Packit 67cb25
Packit 67cb25
   using 48-bit unsigned arithmetic, with a = 0x5DEECE66D , c = 0xB
Packit 67cb25
   and m = 2^48. The seed specifies the upper 32 bits of the initial
Packit 67cb25
   value, x_1, with the lower 16 bits set to 0x330E.
Packit 67cb25
Packit 67cb25
   The theoretical value of x_{10001} is 244131582646046.
Packit 67cb25
Packit 67cb25
   The period of this generator is ? FIXME (probably around 2^48). */
Packit 67cb25
Packit 67cb25
static inline void rand48_advance (void *vstate);
Packit 67cb25
static unsigned long int rand48_get (void *vstate);
Packit 67cb25
static double rand48_get_double (void *vstate);
Packit 67cb25
static void rand48_set (void *state, unsigned long int s);
Packit 67cb25
Packit 67cb25
static const unsigned short int a0 = 0xE66D ;
Packit 67cb25
static const unsigned short int a1 = 0xDEEC ;
Packit 67cb25
static const unsigned short int a2 = 0x0005 ;
Packit 67cb25
Packit 67cb25
static const unsigned short int c0 = 0x000B ;
Packit 67cb25
Packit 67cb25
typedef struct
Packit 67cb25
  {
Packit 67cb25
    unsigned short int x0, x1, x2;
Packit 67cb25
  }
Packit 67cb25
rand48_state_t;
Packit 67cb25
Packit 67cb25
static inline void
Packit 67cb25
rand48_advance (void *vstate)
Packit 67cb25
{
Packit 67cb25
  rand48_state_t *state = (rand48_state_t *) vstate;
Packit 67cb25
Packit 67cb25
  /* work with unsigned long ints throughout to get correct integer
Packit 67cb25
     promotions of any unsigned short ints */
Packit 67cb25
Packit 67cb25
  const unsigned long int x0 = (unsigned long int) state->x0 ;
Packit 67cb25
  const unsigned long int x1 = (unsigned long int) state->x1 ;
Packit 67cb25
  const unsigned long int x2 = (unsigned long int) state->x2 ;
Packit 67cb25
Packit 67cb25
  unsigned long int a ;
Packit 67cb25
  
Packit 67cb25
  a = a0 * x0 + c0 ;
Packit 67cb25
  state->x0 = (a & 0xFFFF) ;
Packit 67cb25
 
Packit 67cb25
  a >>= 16 ;
Packit 67cb25
Packit 67cb25
  /* although the next line may overflow we only need the top 16 bits
Packit 67cb25
     in the following stage, so it does not matter */
Packit 67cb25
Packit 67cb25
  a += a0 * x1 + a1 * x0 ; 
Packit 67cb25
  state->x1 = (a & 0xFFFF) ;
Packit 67cb25
Packit 67cb25
  a >>= 16 ;
Packit 67cb25
  a += a0 * x2 + a1 * x1 + a2 * x0 ;
Packit 67cb25
  state->x2 = (a & 0xFFFF) ;
Packit 67cb25
}
Packit 67cb25
Packit 67cb25
static unsigned long int 
Packit 67cb25
rand48_get (void *vstate)
Packit 67cb25
{
Packit 67cb25
  unsigned long int x1, x2;
Packit 67cb25
Packit 67cb25
  rand48_state_t *state = (rand48_state_t *) vstate;
Packit 67cb25
  rand48_advance (state) ;
Packit 67cb25
Packit 67cb25
  x2 = (unsigned long int) state->x2;
Packit 67cb25
  x1 = (unsigned long int) state->x1;
Packit 67cb25
Packit 67cb25
  return (x2 << 16) + x1;
Packit 67cb25
}
Packit 67cb25
Packit 67cb25
static double
Packit 67cb25
rand48_get_double (void * vstate)
Packit 67cb25
{
Packit 67cb25
  rand48_state_t *state = (rand48_state_t *) vstate;
Packit 67cb25
Packit 67cb25
  rand48_advance (state) ;  
Packit 67cb25
Packit 67cb25
  return (ldexp((double) state->x2, -16)
Packit 67cb25
          + ldexp((double) state->x1, -32) 
Packit 67cb25
          + ldexp((double) state->x0, -48)) ;
Packit 67cb25
}
Packit 67cb25
Packit 67cb25
static void
Packit 67cb25
rand48_set (void *vstate, unsigned long int s)
Packit 67cb25
{
Packit 67cb25
  rand48_state_t *state = (rand48_state_t *) vstate;
Packit 67cb25
Packit 67cb25
  if (s == 0)  /* default seed */
Packit 67cb25
    {
Packit 67cb25
      state->x0 = 0x330E ;
Packit 67cb25
      state->x1 = 0xABCD ;
Packit 67cb25
      state->x2 = 0x1234 ;
Packit 67cb25
    }
Packit 67cb25
  else 
Packit 67cb25
    {
Packit 67cb25
      state->x0 = 0x330E ;
Packit 67cb25
      state->x1 = s & 0xFFFF ;
Packit 67cb25
      state->x2 = (s >> 16) & 0xFFFF ;
Packit 67cb25
    }
Packit 67cb25
Packit 67cb25
  return;
Packit 67cb25
}
Packit 67cb25
Packit 67cb25
static const gsl_rng_type rand48_type =
Packit 67cb25
{"rand48",                      /* name */
Packit 67cb25
 0xffffffffUL,                  /* RAND_MAX */
Packit 67cb25
 0,                             /* RAND_MIN */
Packit 67cb25
 sizeof (rand48_state_t),
Packit 67cb25
 &rand48_set,
Packit 67cb25
 &rand48_get,
Packit 67cb25
 &rand48_get_double
Packit 67cb25
};
Packit 67cb25
Packit 67cb25
const gsl_rng_type *gsl_rng_rand48 = &rand48_type;