gsl_rng__rng.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. /* rng/rng.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_errno.h"
  24. #include "gsl_rng.h"
  25. gsl_rng *
  26. gsl_rng_alloc (const gsl_rng_type * T)
  27. {
  28. gsl_rng *r = (gsl_rng *) malloc (sizeof (gsl_rng));
  29. if (r == 0)
  30. {
  31. GSL_ERROR_VAL ("failed to allocate space for rng struct",
  32. GSL_ENOMEM, 0);
  33. };
  34. r->state = malloc (T->size);
  35. if (r->state == 0)
  36. {
  37. free (r); /* exception in constructor, avoid memory leak */
  38. GSL_ERROR_VAL ("failed to allocate space for rng state",
  39. GSL_ENOMEM, 0);
  40. };
  41. r->type = T;
  42. gsl_rng_set (r, gsl_rng_default_seed); /* seed the generator */
  43. return r;
  44. }
  45. int
  46. gsl_rng_memcpy (gsl_rng * dest, const gsl_rng * src)
  47. {
  48. if (dest->type != src->type)
  49. {
  50. GSL_ERROR ("generators must be of the same type", GSL_EINVAL);
  51. }
  52. memcpy (dest->state, src->state, src->type->size);
  53. return GSL_SUCCESS;
  54. }
  55. gsl_rng *
  56. gsl_rng_clone (const gsl_rng * q)
  57. {
  58. gsl_rng *r = (gsl_rng *) malloc (sizeof (gsl_rng));
  59. if (r == 0)
  60. {
  61. GSL_ERROR_VAL ("failed to allocate space for rng struct",
  62. GSL_ENOMEM, 0);
  63. };
  64. r->state = malloc (q->type->size);
  65. if (r->state == 0)
  66. {
  67. free (r); /* exception in constructor, avoid memory leak */
  68. GSL_ERROR_VAL ("failed to allocate space for rng state",
  69. GSL_ENOMEM, 0);
  70. };
  71. r->type = q->type;
  72. memcpy (r->state, q->state, q->type->size);
  73. return r;
  74. }
  75. void
  76. gsl_rng_set (const gsl_rng * r, unsigned long int seed)
  77. {
  78. (r->type->set) (r->state, seed);
  79. }
  80. #ifndef HIDE_INLINE_STATIC
  81. unsigned long int
  82. gsl_rng_get (const gsl_rng * r)
  83. {
  84. return (r->type->get) (r->state);
  85. }
  86. double
  87. gsl_rng_uniform (const gsl_rng * r)
  88. {
  89. return (r->type->get_double) (r->state);
  90. }
  91. double
  92. gsl_rng_uniform_pos (const gsl_rng * r)
  93. {
  94. double x ;
  95. do
  96. {
  97. x = (r->type->get_double) (r->state) ;
  98. }
  99. while (x == 0) ;
  100. return x ;
  101. }
  102. /* Note: to avoid integer overflow in (range+1) we work with scale =
  103. range/n = (max-min)/n rather than scale=(max-min+1)/n, this reduces
  104. efficiency slightly but avoids having to check for the out of range
  105. value. Note that range is typically O(2^32) so the addition of 1
  106. is negligible in most usage. */
  107. unsigned long int
  108. gsl_rng_uniform_int (const gsl_rng * r, unsigned long int n)
  109. {
  110. unsigned long int offset = r->type->min;
  111. unsigned long int range = r->type->max - offset;
  112. unsigned long int scale;
  113. unsigned long int k;
  114. if (n > range || n == 0)
  115. {
  116. GSL_ERROR_VAL ("invalid n, either 0 or exceeds maximum value of generator",
  117. GSL_EINVAL, 0) ;
  118. }
  119. scale = range / n;
  120. do
  121. {
  122. k = (((r->type->get) (r->state)) - offset) / scale;
  123. }
  124. while (k >= n);
  125. return k;
  126. }
  127. #endif
  128. unsigned long int
  129. gsl_rng_max (const gsl_rng * r)
  130. {
  131. return r->type->max;
  132. }
  133. unsigned long int
  134. gsl_rng_min (const gsl_rng * r)
  135. {
  136. return r->type->min;
  137. }
  138. const char *
  139. gsl_rng_name (const gsl_rng * r)
  140. {
  141. return r->type->name;
  142. }
  143. size_t
  144. gsl_rng_size (const gsl_rng * r)
  145. {
  146. return r->type->size;
  147. }
  148. void *
  149. gsl_rng_state (const gsl_rng * r)
  150. {
  151. return r->state;
  152. }
  153. void
  154. gsl_rng_print_state (const gsl_rng * r)
  155. {
  156. size_t i;
  157. unsigned char *p = (unsigned char *) (r->state);
  158. const size_t n = r->type->size;
  159. for (i = 0; i < n; i++)
  160. {
  161. /* FIXME: we're assuming that a char is 8 bits */
  162. printf ("%.2x", *(p + i));
  163. }
  164. }
  165. void
  166. gsl_rng_free (gsl_rng * r)
  167. {
  168. free (r->state);
  169. free (r);
  170. }