randomkit.h 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. /* Random kit 1.3 */
  2. /*
  3. * Copyright (c) 2003-2005, Jean-Sebastien Roy (js@jeannot.org)
  4. *
  5. * Permission is hereby granted, free of charge, to any person obtaining a
  6. * copy of this software and associated documentation files (the
  7. * "Software"), to deal in the Software without restriction, including
  8. * without limitation the rights to use, copy, modify, merge, publish,
  9. * distribute, sublicense, and/or sell copies of the Software, and to
  10. * permit persons to whom the Software is furnished to do so, subject to
  11. * the following conditions:
  12. *
  13. * The above copyright notice and this permission notice shall be included
  14. * in all copies or substantial portions of the Software.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  17. * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  18. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  19. * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
  20. * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
  21. * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
  22. * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  23. */
  24. /* @(#) $Jeannot: randomkit.h,v 1.24 2005/07/21 22:14:09 js Exp $ */
  25. /*
  26. * Typical use:
  27. *
  28. * {
  29. * rk_state state;
  30. * unsigned long seed = 1, random_value;
  31. *
  32. * rk_seed(seed, &state); // Initialize the RNG
  33. * ...
  34. * random_value = rk_random(&state); // Generate random values in [0..RK_MAX]
  35. * }
  36. *
  37. * Instead of rk_seed, you can use rk_randomseed which will get a random seed
  38. * from /dev/urandom (or the clock, if /dev/urandom is unavailable):
  39. *
  40. * {
  41. * rk_state state;
  42. * unsigned long random_value;
  43. *
  44. * rk_randomseed(&state); // Initialize the RNG with a random seed
  45. * ...
  46. * random_value = rk_random(&state); // Generate random values in [0..RK_MAX]
  47. * }
  48. */
  49. /*
  50. * Useful macro:
  51. * RK_DEV_RANDOM: the device used for random seeding.
  52. * defaults to "/dev/urandom"
  53. */
  54. #ifndef _RANDOMKIT_
  55. #define _RANDOMKIT_
  56. #include <stddef.h>
  57. #include <numpy/npy_common.h>
  58. #define RK_STATE_LEN 624
  59. typedef struct rk_state_
  60. {
  61. unsigned long key[RK_STATE_LEN];
  62. int pos;
  63. int has_gauss; /* !=0: gauss contains a gaussian deviate */
  64. double gauss;
  65. /* The rk_state structure has been extended to store the following
  66. * information for the binomial generator. If the input values of n or p
  67. * are different than nsave and psave, then the other parameters will be
  68. * recomputed. RTK 2005-09-02 */
  69. int has_binomial; /* !=0: following parameters initialized for
  70. binomial */
  71. double psave;
  72. long nsave;
  73. double r;
  74. double q;
  75. double fm;
  76. long m;
  77. double p1;
  78. double xm;
  79. double xl;
  80. double xr;
  81. double c;
  82. double laml;
  83. double lamr;
  84. double p2;
  85. double p3;
  86. double p4;
  87. }
  88. rk_state;
  89. typedef enum {
  90. RK_NOERR = 0, /* no error */
  91. RK_ENODEV = 1, /* no RK_DEV_RANDOM device */
  92. RK_ERR_MAX = 2
  93. } rk_error;
  94. /* error strings */
  95. extern char *rk_strerror[RK_ERR_MAX];
  96. /* Maximum generated random value */
  97. #define RK_MAX 0xFFFFFFFFUL
  98. #ifdef __cplusplus
  99. extern "C" {
  100. #endif
  101. /*
  102. * Initialize the RNG state using the given seed.
  103. */
  104. extern void rk_seed(unsigned long seed, rk_state *state);
  105. /*
  106. * Initialize the RNG state using a random seed.
  107. * Uses /dev/random or, when unavailable, the clock (see randomkit.c).
  108. * Returns RK_NOERR when no errors occurs.
  109. * Returns RK_ENODEV when the use of RK_DEV_RANDOM failed (for example because
  110. * there is no such device). In this case, the RNG was initialized using the
  111. * clock.
  112. */
  113. extern rk_error rk_randomseed(rk_state *state);
  114. /*
  115. * Returns a random unsigned long between 0 and RK_MAX inclusive
  116. */
  117. extern unsigned long rk_random(rk_state *state);
  118. /*
  119. * Returns a random long between 0 and LONG_MAX inclusive
  120. */
  121. extern long rk_long(rk_state *state);
  122. /*
  123. * Returns a random unsigned long between 0 and ULONG_MAX inclusive
  124. */
  125. extern unsigned long rk_ulong(rk_state *state);
  126. /*
  127. * Returns a random unsigned long between 0 and max inclusive.
  128. */
  129. extern unsigned long rk_interval(unsigned long max, rk_state *state);
  130. /*
  131. * Fills an array with cnt random npy_uint64 between off and off + rng
  132. * inclusive. The numbers wrap if rng is sufficiently large.
  133. */
  134. extern void rk_random_uint64(npy_uint64 off, npy_uint64 rng, npy_intp cnt,
  135. npy_uint64 *out, rk_state *state);
  136. /*
  137. * Fills an array with cnt random npy_uint32 between off and off + rng
  138. * inclusive. The numbers wrap if rng is sufficiently large.
  139. */
  140. extern void rk_random_uint32(npy_uint32 off, npy_uint32 rng, npy_intp cnt,
  141. npy_uint32 *out, rk_state *state);
  142. /*
  143. * Fills an array with cnt random npy_uint16 between off and off + rng
  144. * inclusive. The numbers wrap if rng is sufficiently large.
  145. */
  146. extern void rk_random_uint16(npy_uint16 off, npy_uint16 rng, npy_intp cnt,
  147. npy_uint16 *out, rk_state *state);
  148. /*
  149. * Fills an array with cnt random npy_uint8 between off and off + rng
  150. * inclusive. The numbers wrap if rng is sufficiently large.
  151. */
  152. extern void rk_random_uint8(npy_uint8 off, npy_uint8 rng, npy_intp cnt,
  153. npy_uint8 *out, rk_state *state);
  154. /*
  155. * Fills an array with cnt random npy_bool between off and off + rng
  156. * inclusive. It is assumed tha npy_bool as the same size as npy_uint8.
  157. */
  158. extern void rk_random_bool(npy_bool off, npy_bool rng, npy_intp cnt,
  159. npy_bool *out, rk_state *state);
  160. /*
  161. * Returns a random double between 0.0 and 1.0, 1.0 excluded.
  162. */
  163. extern double rk_double(rk_state *state);
  164. /*
  165. * fill the buffer with size random bytes
  166. */
  167. extern void rk_fill(void *buffer, size_t size, rk_state *state);
  168. /*
  169. * fill the buffer with randombytes from the random device
  170. * Returns RK_ENODEV if the device is unavailable, or RK_NOERR if it is
  171. * On Unix, if strong is defined, RK_DEV_RANDOM is used. If not, RK_DEV_URANDOM
  172. * is used instead. This parameter has no effect on Windows.
  173. * Warning: on most unixes RK_DEV_RANDOM will wait for enough entropy to answer
  174. * which can take a very long time on quiet systems.
  175. */
  176. extern rk_error rk_devfill(void *buffer, size_t size, int strong);
  177. /*
  178. * fill the buffer using rk_devfill if the random device is available and using
  179. * rk_fill if it is not
  180. * parameters have the same meaning as rk_fill and rk_devfill
  181. * Returns RK_ENODEV if the device is unavailable, or RK_NOERR if it is
  182. */
  183. extern rk_error rk_altfill(void *buffer, size_t size, int strong,
  184. rk_state *state);
  185. /*
  186. * return a random gaussian deviate with variance unity and zero mean.
  187. */
  188. extern double rk_gauss(rk_state *state);
  189. #ifdef __cplusplus
  190. }
  191. #endif
  192. #endif /* _RANDOMKIT_ */