gsl_rng__default.c 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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. #include "gsl__config.h"
  20. #include <stdlib.h>
  21. #include <stdio.h>
  22. #include <string.h>
  23. #include "gsl_rng.h"
  24. #include "gsl_errno.h"
  25. /* The initial defaults are defined in the file mt.c, so we can get
  26. access to the static parts of the default generator. */
  27. const gsl_rng_type *
  28. gsl_rng_env_setup (void)
  29. {
  30. unsigned long int seed = 0;
  31. const char *p = getenv ("GSL_RNG_TYPE");
  32. if (p)
  33. {
  34. const gsl_rng_type **t, **t0 = gsl_rng_types_setup ();
  35. gsl_rng_default = 0;
  36. /* check GSL_RNG_TYPE against the names of all the generators */
  37. for (t = t0; *t != 0; t++)
  38. {
  39. if (strcmp (p, (*t)->name) == 0)
  40. {
  41. gsl_rng_default = *t;
  42. break;
  43. }
  44. }
  45. if (gsl_rng_default == 0)
  46. {
  47. int i = 0;
  48. fprintf (stderr, "GSL_RNG_TYPE=%s not recognized\n", p);
  49. fprintf (stderr, "Valid generator types are:\n");
  50. for (t = t0; *t != 0; t++)
  51. {
  52. fprintf (stderr, " %18s", (*t)->name);
  53. if ((++i) % 4 == 0)
  54. {
  55. fputc ('\n', stderr);
  56. }
  57. }
  58. fputc ('\n', stderr);
  59. GSL_ERROR_VAL ("unknown generator", GSL_EINVAL, 0);
  60. }
  61. fprintf (stderr, "GSL_RNG_TYPE=%s\n", gsl_rng_default->name);
  62. }
  63. else
  64. {
  65. gsl_rng_default = gsl_rng_mt19937;
  66. }
  67. p = getenv ("GSL_RNG_SEED");
  68. if (p)
  69. {
  70. seed = strtoul (p, 0, 0);
  71. fprintf (stderr, "GSL_RNG_SEED=%lu\n", seed);
  72. };
  73. gsl_rng_default_seed = seed;
  74. return gsl_rng_default;
  75. }