| 1 | /* rng/default.c |
| 2 | * |
| 3 | * Copyright (C) 1996, 1997, 1998, 1999, 2000, 2007 James Theiler, Brian Gough |
| 4 | * |
| 5 | * This program is free software; you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License as published by |
| 7 | * the Free Software Foundation; either version 3 of the License, or (at |
| 8 | * your option) any later version. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, but |
| 11 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 13 | * General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License |
| 16 | * along with this program; if not, write to the Free Software |
| 17 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 18 | */ |
| 19 | |
| 20 | #include "gsl__config.h" |
| 21 | #include <stdlib.h> |
| 22 | #include <stdio.h> |
| 23 | #include <string.h> |
| 24 | #include "gsl_rng.h" |
| 25 | #include "gsl_errno.h" |
| 26 | |
| 27 | /* The initial defaults are defined in the file mt.c, so we can get |
| 28 | access to the static parts of the default generator. */ |
| 29 | |
| 30 | const gsl_rng_type * |
| 31 | gsl_rng_env_setup (void) |
| 32 | { |
| 33 | unsigned long int seed = 0; |
| 34 | const char *p = getenv ("GSL_RNG_TYPE"); |
| 35 | |
| 36 | if (p) |
| 37 | { |
| 38 | const gsl_rng_type **t, **t0 = gsl_rng_types_setup (); |
| 39 | |
| 40 | gsl_rng_default = 0; |
| 41 | |
| 42 | /* check GSL_RNG_TYPE against the names of all the generators */ |
| 43 | |
| 44 | for (t = t0; *t != 0; t++) |
| 45 | { |
| 46 | if (strcmp (p, (*t)->name) == 0) |
| 47 | { |
| 48 | gsl_rng_default = *t; |
| 49 | break; |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | if (gsl_rng_default == 0) |
| 54 | { |
| 55 | int i = 0; |
| 56 | |
| 57 | fprintf (stderr, "GSL_RNG_TYPE=%s not recognized\n", p); |
| 58 | fprintf (stderr, "Valid generator types are:\n"); |
| 59 | |
| 60 | for (t = t0; *t != 0; t++) |
| 61 | { |
| 62 | fprintf (stderr, " %18s", (*t)->name); |
| 63 | |
| 64 | if ((++i) % 4 == 0) |
| 65 | { |
| 66 | fputc ('\n', stderr); |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | fputc ('\n', stderr); |
| 71 | |
| 72 | GSL_ERROR_VAL ("unknown generator", GSL_EINVAL, 0); |
| 73 | } |
| 74 | |
| 75 | fprintf (stderr, "GSL_RNG_TYPE=%s\n", gsl_rng_default->name); |
| 76 | } |
| 77 | else |
| 78 | { |
| 79 | gsl_rng_default = gsl_rng_mt19937; |
| 80 | } |
| 81 | |
| 82 | p = getenv ("GSL_RNG_SEED"); |
| 83 | |
| 84 | if (p) |
| 85 | { |
| 86 | seed = strtoul (p, 0, 0); |
| 87 | fprintf (stderr, "GSL_RNG_SEED=%lu\n", seed); |
| 88 | }; |
| 89 | |
| 90 | gsl_rng_default_seed = seed; |
| 91 | |
| 92 | return gsl_rng_default; |
| 93 | } |