Blame rng/rng.c

Packit 67cb25
/* rng/rng.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 <stdio.h>
Packit 67cb25
#include <string.h>
Packit 67cb25
#include <gsl/gsl_errno.h>
Packit 67cb25
#include <gsl/gsl_rng.h>
Packit 67cb25
Packit 67cb25
gsl_rng *
Packit 67cb25
gsl_rng_alloc (const gsl_rng_type * T)
Packit 67cb25
{
Packit 67cb25
Packit 67cb25
  gsl_rng *r = (gsl_rng *) malloc (sizeof (gsl_rng));
Packit 67cb25
Packit 67cb25
  if (r == 0)
Packit 67cb25
    {
Packit 67cb25
      GSL_ERROR_VAL ("failed to allocate space for rng struct",
Packit 67cb25
                        GSL_ENOMEM, 0);
Packit 67cb25
    };
Packit 67cb25
Packit 67cb25
  r->state = calloc (1, T->size);
Packit 67cb25
Packit 67cb25
  if (r->state == 0)
Packit 67cb25
    {
Packit 67cb25
      free (r);         /* exception in constructor, avoid memory leak */
Packit 67cb25
Packit 67cb25
      GSL_ERROR_VAL ("failed to allocate space for rng state",
Packit 67cb25
                        GSL_ENOMEM, 0);
Packit 67cb25
    };
Packit 67cb25
Packit 67cb25
  r->type = T;
Packit 67cb25
Packit 67cb25
  gsl_rng_set (r, gsl_rng_default_seed);        /* seed the generator */
Packit 67cb25
Packit 67cb25
  return r;
Packit 67cb25
}
Packit 67cb25
Packit 67cb25
int
Packit 67cb25
gsl_rng_memcpy (gsl_rng * dest, const gsl_rng * src)
Packit 67cb25
{
Packit 67cb25
  if (dest->type != src->type)
Packit 67cb25
    {
Packit 67cb25
      GSL_ERROR ("generators must be of the same type", GSL_EINVAL);
Packit 67cb25
    }
Packit 67cb25
Packit 67cb25
  memcpy (dest->state, src->state, src->type->size);
Packit 67cb25
Packit 67cb25
  return GSL_SUCCESS;
Packit 67cb25
}
Packit 67cb25
Packit 67cb25
gsl_rng *
Packit 67cb25
gsl_rng_clone (const gsl_rng * q)
Packit 67cb25
{
Packit 67cb25
  gsl_rng *r = (gsl_rng *) malloc (sizeof (gsl_rng));
Packit 67cb25
Packit 67cb25
  if (r == 0)
Packit 67cb25
    {
Packit 67cb25
      GSL_ERROR_VAL ("failed to allocate space for rng struct",
Packit 67cb25
                        GSL_ENOMEM, 0);
Packit 67cb25
    };
Packit 67cb25
Packit 67cb25
  r->state = malloc (q->type->size);
Packit 67cb25
Packit 67cb25
  if (r->state == 0)
Packit 67cb25
    {
Packit 67cb25
      free (r);         /* exception in constructor, avoid memory leak */
Packit 67cb25
Packit 67cb25
      GSL_ERROR_VAL ("failed to allocate space for rng state",
Packit 67cb25
                        GSL_ENOMEM, 0);
Packit 67cb25
    };
Packit 67cb25
Packit 67cb25
  r->type = q->type;
Packit 67cb25
Packit 67cb25
  memcpy (r->state, q->state, q->type->size);
Packit 67cb25
Packit 67cb25
  return r;
Packit 67cb25
}
Packit 67cb25
Packit 67cb25
void
Packit 67cb25
gsl_rng_set (const gsl_rng * r, unsigned long int seed)
Packit 67cb25
{
Packit 67cb25
  (r->type->set) (r->state, seed);
Packit 67cb25
}
Packit 67cb25
Packit 67cb25
unsigned long int
Packit 67cb25
gsl_rng_max (const gsl_rng * r)
Packit 67cb25
{
Packit 67cb25
  return r->type->max;
Packit 67cb25
}
Packit 67cb25
Packit 67cb25
unsigned long int
Packit 67cb25
gsl_rng_min (const gsl_rng * r)
Packit 67cb25
{
Packit 67cb25
  return r->type->min;
Packit 67cb25
}
Packit 67cb25
Packit 67cb25
const char *
Packit 67cb25
gsl_rng_name (const gsl_rng * r)
Packit 67cb25
{
Packit 67cb25
  return r->type->name;
Packit 67cb25
}
Packit 67cb25
Packit 67cb25
size_t
Packit 67cb25
gsl_rng_size (const gsl_rng * r)
Packit 67cb25
{
Packit 67cb25
  return r->type->size;
Packit 67cb25
}
Packit 67cb25
Packit 67cb25
void *
Packit 67cb25
gsl_rng_state (const gsl_rng * r)
Packit 67cb25
{
Packit 67cb25
  return r->state;
Packit 67cb25
}
Packit 67cb25
Packit 67cb25
void
Packit 67cb25
gsl_rng_print_state (const gsl_rng * r)
Packit 67cb25
{
Packit 67cb25
  size_t i;
Packit 67cb25
  unsigned char *p = (unsigned char *) (r->state);
Packit 67cb25
  const size_t n = r->type->size;
Packit 67cb25
Packit 67cb25
  for (i = 0; i < n; i++)
Packit 67cb25
    {
Packit 67cb25
      /* FIXME: we're assuming that a char is 8 bits */
Packit 67cb25
      printf ("%.2x", *(p + i));
Packit 67cb25
    }
Packit 67cb25
Packit 67cb25
}
Packit 67cb25
Packit 67cb25
void
Packit 67cb25
gsl_rng_free (gsl_rng * r)
Packit 67cb25
{
Packit 67cb25
  RETURN_IF_NULL (r);
Packit 67cb25
  free (r->state);
Packit 67cb25
  free (r);
Packit 67cb25
}