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