randomkit.h 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. /* Random kit 1.3 */
  2. /*
  3. * Copyright (c) 2003-2005, Jean-Sebastien Roy ([email protected])
  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. /*
  25. * Typical use:
  26. *
  27. * {
  28. * rk_state state;
  29. * unsigned long seed = 1, random_value;
  30. *
  31. * rk_seed(seed, &state); // Initialize the RNG
  32. * ...
  33. * random_value = rk_random(&state); // Generate random values in [0..RK_MAX]
  34. * }
  35. *
  36. * Instead of rk_seed, you can use rk_randomseed which will get a random seed
  37. * from /dev/urandom (or the clock, if /dev/urandom is unavailable):
  38. *
  39. * {
  40. * rk_state state;
  41. * unsigned long random_value;
  42. *
  43. * rk_randomseed(&state); // Initialize the RNG with a random seed
  44. * ...
  45. * random_value = rk_random(&state); // Generate random values in [0..RK_MAX]
  46. * }
  47. */
  48. /*
  49. * Useful macro:
  50. * RK_DEV_RANDOM: the device used for random seeding.
  51. * defaults to "/dev/urandom"
  52. */
  53. #pragma once
  54. #include <stddef.h>
  55. #define RK_STATE_LEN 624
  56. typedef struct rk_state_
  57. {
  58. unsigned long key[RK_STATE_LEN];
  59. int pos;
  60. }
  61. rk_state;
  62. /* Maximum generated random value */
  63. #define RK_MAX 0xFFFFFFFFUL
  64. #ifdef __cplusplus
  65. extern "C" {
  66. #endif
  67. /*
  68. * Initialize the RNG state using the given seed.
  69. */
  70. extern void rk_seed(unsigned long seed, rk_state *state);
  71. /*
  72. * Returns a random unsigned long between 0 and RK_MAX inclusive
  73. */
  74. extern unsigned long rk_random(rk_state *state);
  75. /*
  76. * Returns a random unsigned long between 0 and max inclusive.
  77. */
  78. extern unsigned long rk_interval(unsigned long max, rk_state *state);
  79. #ifdef __cplusplus
  80. }
  81. #endif