random.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. #ifndef SCM_RANDOM_H
  2. #define SCM_RANDOM_H
  3. /* Copyright 1999-2001,2006,2008,2010,2018
  4. Free Software Foundation, Inc.
  5. This file is part of Guile.
  6. Guile is free software: you can redistribute it and/or modify it
  7. under the terms of the GNU Lesser General Public License as published
  8. by the Free Software Foundation, either version 3 of the License, or
  9. (at your option) any later version.
  10. Guile is distributed in the hope that it will be useful, but WITHOUT
  11. ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  12. FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
  13. License for more details.
  14. You should have received a copy of the GNU Lesser General Public
  15. License along with Guile. If not, see
  16. <https://www.gnu.org/licenses/>. */
  17. #include <libguile/error.h>
  18. /*
  19. * A plugin interface for RNGs
  20. *
  21. * Using this interface, it is possible for the application to tell
  22. * libguile to use a different RNG. This is desirable if it is
  23. * necessary to use the same RNG everywhere in the application in
  24. * order to prevent interference, if the application uses RNG
  25. * hardware, or if the application has special demands on the RNG.
  26. *
  27. * Look how the default generator is "plugged in" in scm_init_random().
  28. */
  29. typedef struct scm_t_rstate {
  30. struct scm_t_rng *rng;
  31. double normal_next; /* For scm_c_normal01 */
  32. /* Custom fields follow here */
  33. } scm_t_rstate;
  34. typedef struct scm_t_rng {
  35. size_t rstate_size; /* size of random state */
  36. uint32_t (*random_bits) (scm_t_rstate *state); /* gives 32 random bits */
  37. void (*init_rstate) (scm_t_rstate *state, const char *seed, int n);
  38. scm_t_rstate *(*copy_rstate) (scm_t_rstate *state);
  39. void (*from_datum) (scm_t_rstate *state, SCM datum);
  40. SCM (*to_datum) (scm_t_rstate *state);
  41. } scm_t_rng;
  42. SCM_API scm_t_rng scm_the_rng;
  43. /*
  44. * Random number library functions
  45. */
  46. SCM_API scm_t_rstate *scm_c_make_rstate (const char *, int);
  47. SCM_API scm_t_rstate *scm_c_rstate_from_datum (SCM datum);
  48. SCM_API scm_t_rstate *scm_c_default_rstate (void);
  49. #define scm_c_uniform32(RSTATE) ((RSTATE)->rng->random_bits (RSTATE))
  50. SCM_API double scm_c_uniform01 (scm_t_rstate *);
  51. SCM_API double scm_c_normal01 (scm_t_rstate *);
  52. SCM_API double scm_c_exp1 (scm_t_rstate *);
  53. SCM_API uint32_t scm_c_random (scm_t_rstate *, uint32_t m);
  54. SCM_API uint64_t scm_c_random64 (scm_t_rstate *state, uint64_t m);
  55. SCM_API SCM scm_c_random_bignum (scm_t_rstate *, SCM m);
  56. /*
  57. * Scheme level interface
  58. */
  59. SCM_API scm_t_bits scm_tc16_rstate;
  60. #define SCM_RSTATEP(obj) SCM_SMOB_PREDICATE (scm_tc16_rstate, obj)
  61. #define SCM_RSTATE(obj) ((scm_t_rstate *) SCM_SMOB_DATA (obj))
  62. #define SCM_VALIDATE_RSTATE(pos, v) \
  63. SCM_MAKE_VALIDATE_MSG (pos, v, RSTATEP, "random-generator-state")
  64. SCM_API unsigned char scm_masktab[256];
  65. SCM_API SCM scm_var_random_state;
  66. SCM_API SCM scm_random (SCM n, SCM state);
  67. SCM_API SCM scm_copy_random_state (SCM state);
  68. SCM_API SCM scm_seed_to_random_state (SCM seed);
  69. SCM_API SCM scm_datum_to_random_state (SCM datum);
  70. SCM_API SCM scm_random_state_to_datum (SCM state);
  71. SCM_API SCM scm_random_state_from_platform (void);
  72. SCM_API SCM scm_random_uniform (SCM state);
  73. SCM_API SCM scm_random_solid_sphere_x (SCM v, SCM state);
  74. SCM_API SCM scm_random_hollow_sphere_x (SCM v, SCM state);
  75. SCM_API SCM scm_random_normal (SCM state);
  76. SCM_API SCM scm_random_normal_vector_x (SCM v, SCM state);
  77. SCM_API SCM scm_random_exp (SCM state);
  78. SCM_INTERNAL void scm_init_random (void);
  79. SCM_INTERNAL void scm_i_random_bytes_from_platform (unsigned char *buf, size_t len);
  80. #endif /* SCM_RANDOM_H */