Blame rng/default.c

Packit 67cb25
/* rng/default.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_rng.h>
Packit 67cb25
#include <gsl/gsl_errno.h>
Packit 67cb25
Packit 67cb25
/* The initial defaults are defined in the file mt.c, so we can get
Packit 67cb25
   access to the static parts of the default generator. */
Packit 67cb25
Packit 67cb25
const gsl_rng_type *
Packit 67cb25
gsl_rng_env_setup (void)
Packit 67cb25
{
Packit 67cb25
  unsigned long int seed = 0;
Packit 67cb25
  const char *p = getenv ("GSL_RNG_TYPE");
Packit 67cb25
Packit 67cb25
  if (p)
Packit 67cb25
    {
Packit 67cb25
      const gsl_rng_type **t, **t0 = gsl_rng_types_setup ();
Packit 67cb25
Packit 67cb25
      gsl_rng_default = 0;
Packit 67cb25
Packit 67cb25
      /* check GSL_RNG_TYPE against the names of all the generators */
Packit 67cb25
Packit 67cb25
      for (t = t0; *t != 0; t++)
Packit 67cb25
        {
Packit 67cb25
          if (strcmp (p, (*t)->name) == 0)
Packit 67cb25
            {
Packit 67cb25
              gsl_rng_default = *t;
Packit 67cb25
              break;
Packit 67cb25
            }
Packit 67cb25
        }
Packit 67cb25
Packit 67cb25
      if (gsl_rng_default == 0)
Packit 67cb25
        {
Packit 67cb25
          int i = 0;
Packit 67cb25
Packit 67cb25
          fprintf (stderr, "GSL_RNG_TYPE=%s not recognized\n", p);
Packit 67cb25
          fprintf (stderr, "Valid generator types are:\n");
Packit 67cb25
Packit 67cb25
          for (t = t0; *t != 0; t++)
Packit 67cb25
            {
Packit 67cb25
              fprintf (stderr, " %18s", (*t)->name);
Packit 67cb25
Packit 67cb25
              if ((++i) % 4 == 0)
Packit 67cb25
                {
Packit 67cb25
                  fputc ('\n', stderr);
Packit 67cb25
                }
Packit 67cb25
            }
Packit 67cb25
Packit 67cb25
          fputc ('\n', stderr);
Packit 67cb25
Packit 67cb25
          GSL_ERROR_VAL ("unknown generator", GSL_EINVAL, 0);
Packit 67cb25
        }
Packit 67cb25
Packit 67cb25
      fprintf (stderr, "GSL_RNG_TYPE=%s\n", gsl_rng_default->name);
Packit 67cb25
    }
Packit 67cb25
  else
Packit 67cb25
    {
Packit 67cb25
      gsl_rng_default = gsl_rng_mt19937;
Packit 67cb25
    }
Packit 67cb25
Packit 67cb25
  p = getenv ("GSL_RNG_SEED");
Packit 67cb25
Packit 67cb25
  if (p)
Packit 67cb25
    {
Packit 67cb25
      seed = strtoul (p, 0, 0);
Packit 67cb25
      fprintf (stderr, "GSL_RNG_SEED=%lu\n", seed);
Packit 67cb25
    };
Packit 67cb25
Packit 67cb25
  gsl_rng_default_seed = seed;
Packit 67cb25
Packit 67cb25
  return gsl_rng_default;
Packit 67cb25
}