]> git.donarmstrong.com Git - lilypond.git/blob - guile18/libguile/random.h
Import guile-1.8 as multiple upstream tarball component
[lilypond.git] / guile18 / libguile / random.h
1 /* classes: h_files */
2
3 #ifndef SCM_RANDOM_H
4 #define SCM_RANDOM_H
5
6 /* Copyright (C) 1999,2000,2001, 2006, 2010 Free Software Foundation, Inc.
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License as published by the Free Software Foundation; either
11  * version 2.1 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with this library; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21  */
22
23 \f
24
25 #include "libguile/__scm.h"
26
27 \f
28 /*
29  * A plugin interface for RNGs
30  *
31  * Using this interface, it is possible for the application to tell
32  * libguile to use a different RNG.  This is desirable if it is
33  * necessary to use the same RNG everywhere in the application in
34  * order to prevent interference, if the application uses RNG
35  * hardware, or if the application has special demands on the RNG.
36  *
37  * Look how the default generator is "plugged in" in scm_init_random().
38  */
39
40 typedef struct scm_t_rstate {
41   int reserved0;
42   double reserved1;
43   /* Custom fields follow here */
44 } scm_t_rstate;
45
46 typedef struct scm_t_rng {
47   size_t rstate_size;                               /* size of random state */
48   /* Though this returns an unsigned long, it's only 32 bits of randomness. */
49   unsigned long (*random_bits) (scm_t_rstate *state); /* gives 32 random bits */
50   void (*init_rstate) (scm_t_rstate *state, const char *seed, int n);
51   scm_t_rstate *(*copy_rstate) (scm_t_rstate *state);
52 } scm_t_rng;
53
54 SCM_API scm_t_rng scm_the_rng;
55
56 \f
57 /*
58  * Default RNG
59  */
60 typedef struct scm_t_i_rstate {
61   scm_t_rstate rstate;
62   unsigned long w;
63   unsigned long c;
64 } scm_t_i_rstate;
65
66 /* Though this returns an unsigned long, it's only 32 bits of randomness. */
67 SCM_API unsigned long scm_i_uniform32 (scm_t_i_rstate *);
68 SCM_API void scm_i_init_rstate (scm_t_i_rstate *, const char *seed, int n);
69 SCM_API scm_t_i_rstate *scm_i_copy_rstate (scm_t_i_rstate *);
70
71 \f
72 /*
73  * Random number library functions
74  */
75 SCM_API scm_t_rstate *scm_c_make_rstate (const char *, int);
76 SCM_API scm_t_rstate *scm_c_default_rstate (void);
77 #define scm_c_uniform32(RSTATE) scm_the_rng.random_bits (RSTATE)
78 SCM_API double scm_c_uniform01 (scm_t_rstate *);
79 SCM_API double scm_c_normal01 (scm_t_rstate *);
80 SCM_API double scm_c_exp1 (scm_t_rstate *);
81 /* Though this returns an unsigned long, it's only 32 bits of randomness. */
82 SCM_API unsigned long scm_c_random (scm_t_rstate *, unsigned long m);
83 /* This one returns 64 bits of randomness. */
84 SCM_API scm_t_uint64 scm_c_random64 (scm_t_rstate *state, scm_t_uint64 m);
85 SCM_API SCM scm_c_random_bignum (scm_t_rstate *, SCM m);
86
87 \f
88 /*
89  * Scheme level interface
90  */
91 SCM_API scm_t_bits scm_tc16_rstate;
92 #define SCM_RSTATEP(obj) SCM_SMOB_PREDICATE (scm_tc16_rstate, obj)
93 #define SCM_RSTATE(obj)  ((scm_t_rstate *) SCM_SMOB_DATA (obj))
94
95 SCM_API unsigned char scm_masktab[256];
96
97 SCM_API SCM scm_var_random_state;
98 SCM_API SCM scm_random (SCM n, SCM state);
99 SCM_API SCM scm_copy_random_state (SCM state);
100 SCM_API SCM scm_seed_to_random_state (SCM seed);
101 SCM_API SCM scm_random_uniform (SCM state);
102 SCM_API SCM scm_random_solid_sphere_x (SCM v, SCM state);
103 SCM_API SCM scm_random_hollow_sphere_x (SCM v, SCM state);
104 SCM_API SCM scm_random_normal (SCM state);
105 SCM_API SCM scm_random_normal_vector_x (SCM v, SCM state);
106 SCM_API SCM scm_random_exp (SCM state);
107 SCM_API void scm_init_random (void);
108
109 #endif  /* SCM_RANDOM_H */
110
111 /*
112   Local Variables:
113   c-file-style: "gnu"
114   End:
115 */