random.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. /* classes: h_files */
  2. #ifndef SCM_RANDOM_H
  3. #define SCM_RANDOM_H
  4. /* Copyright (C) 1999,2000,2001, 2006, 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
  8. * License as published by the Free Software Foundation; either
  9. * version 2.1 of the License, or (at your option) any later version.
  10. *
  11. * This library is distributed in the hope that it will be useful,
  12. * but 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 02110-1301 USA
  19. */
  20. #include "libguile/__scm.h"
  21. /*
  22. * A plugin interface for RNGs
  23. *
  24. * Using this interface, it is possible for the application to tell
  25. * libguile to use a different RNG. This is desirable if it is
  26. * necessary to use the same RNG everywhere in the application in
  27. * order to prevent interference, if the application uses RNG
  28. * hardware, or if the application has special demands on the RNG.
  29. *
  30. * Look how the default generator is "plugged in" in scm_init_random().
  31. */
  32. typedef struct scm_t_rstate {
  33. int reserved0;
  34. double reserved1;
  35. /* Custom fields follow here */
  36. } scm_t_rstate;
  37. typedef struct scm_t_rng {
  38. size_t rstate_size; /* size of random state */
  39. /* Though this returns an unsigned long, it's only 32 bits of randomness. */
  40. unsigned long (*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. } scm_t_rng;
  44. SCM_API scm_t_rng scm_the_rng;
  45. /*
  46. * Default RNG
  47. */
  48. typedef struct scm_t_i_rstate {
  49. scm_t_rstate rstate;
  50. unsigned long w;
  51. unsigned long c;
  52. } scm_t_i_rstate;
  53. /* Though this returns an unsigned long, it's only 32 bits of randomness. */
  54. SCM_API unsigned long scm_i_uniform32 (scm_t_i_rstate *);
  55. SCM_API void scm_i_init_rstate (scm_t_i_rstate *, const char *seed, int n);
  56. SCM_API scm_t_i_rstate *scm_i_copy_rstate (scm_t_i_rstate *);
  57. /*
  58. * Random number library functions
  59. */
  60. SCM_API scm_t_rstate *scm_c_make_rstate (const char *, int);
  61. SCM_API scm_t_rstate *scm_c_default_rstate (void);
  62. #define scm_c_uniform32(RSTATE) scm_the_rng.random_bits (RSTATE)
  63. SCM_API double scm_c_uniform01 (scm_t_rstate *);
  64. SCM_API double scm_c_normal01 (scm_t_rstate *);
  65. SCM_API double scm_c_exp1 (scm_t_rstate *);
  66. /* Though this returns an unsigned long, it's only 32 bits of randomness. */
  67. SCM_API unsigned long scm_c_random (scm_t_rstate *, unsigned long m);
  68. /* This one returns 64 bits of randomness. */
  69. SCM_API scm_t_uint64 scm_c_random64 (scm_t_rstate *state, scm_t_uint64 m);
  70. SCM_API SCM scm_c_random_bignum (scm_t_rstate *, SCM m);
  71. /*
  72. * Scheme level interface
  73. */
  74. SCM_API scm_t_bits scm_tc16_rstate;
  75. #define SCM_RSTATEP(obj) SCM_SMOB_PREDICATE (scm_tc16_rstate, obj)
  76. #define SCM_RSTATE(obj) ((scm_t_rstate *) SCM_SMOB_DATA (obj))
  77. SCM_API unsigned char scm_masktab[256];
  78. SCM_API SCM scm_var_random_state;
  79. SCM_API SCM scm_random (SCM n, SCM state);
  80. SCM_API SCM scm_copy_random_state (SCM state);
  81. SCM_API SCM scm_seed_to_random_state (SCM seed);
  82. SCM_API SCM scm_random_uniform (SCM state);
  83. SCM_API SCM scm_random_solid_sphere_x (SCM v, SCM state);
  84. SCM_API SCM scm_random_hollow_sphere_x (SCM v, SCM state);
  85. SCM_API SCM scm_random_normal (SCM state);
  86. SCM_API SCM scm_random_normal_vector_x (SCM v, SCM state);
  87. SCM_API SCM scm_random_exp (SCM state);
  88. SCM_API void scm_init_random (void);
  89. #endif /* SCM_RANDOM_H */
  90. /*
  91. Local Variables:
  92. c-file-style: "gnu"
  93. End:
  94. */