random.h 3.6 KB

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