6 /* Copyright (C) 1999,2000,2001, 2006, 2008, 2010 Free Software Foundation, Inc.
8 * This library is free software; you can redistribute it and/or
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.
13 * This library is distributed in the hope that it will be useful, but
14 * 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.
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
26 #include "libguile/__scm.h"
30 * A plugin interface for RNGs
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.
38 * Look how the default generator is "plugged in" in scm_init_random().
41 typedef struct scm_t_rstate
{
42 struct scm_t_rng
*rng
;
43 double normal_next
; /* For scm_c_normal01 */
44 /* Custom fields follow here */
47 typedef struct scm_t_rng
{
48 size_t rstate_size
; /* size of random state */
49 scm_t_uint32 (*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 void (*from_datum
) (scm_t_rstate
*state
, SCM datum
);
53 SCM (*to_datum
) (scm_t_rstate
*state
);
56 SCM_API scm_t_rng scm_the_rng
;
60 * Random number library functions
62 SCM_API scm_t_rstate
*scm_c_make_rstate (const char *, int);
63 SCM_API scm_t_rstate
*scm_c_rstate_from_datum (SCM datum
);
64 SCM_API scm_t_rstate
*scm_c_default_rstate (void);
65 #define scm_c_uniform32(RSTATE) ((RSTATE)->rng->random_bits (RSTATE))
66 SCM_API
double scm_c_uniform01 (scm_t_rstate
*);
67 SCM_API
double scm_c_normal01 (scm_t_rstate
*);
68 SCM_API
double scm_c_exp1 (scm_t_rstate
*);
69 SCM_API scm_t_uint32
scm_c_random (scm_t_rstate
*, scm_t_uint32 m
);
70 SCM_API scm_t_uint64
scm_c_random64 (scm_t_rstate
*state
, scm_t_uint64 m
);
71 SCM_API SCM
scm_c_random_bignum (scm_t_rstate
*, SCM m
);
75 * Scheme level interface
77 SCM_API scm_t_bits scm_tc16_rstate
;
78 #define SCM_RSTATEP(obj) SCM_SMOB_PREDICATE (scm_tc16_rstate, obj)
79 #define SCM_RSTATE(obj) ((scm_t_rstate *) SCM_SMOB_DATA (obj))
81 SCM_API
unsigned char scm_masktab
[256];
83 SCM_API SCM scm_var_random_state
;
84 SCM_API SCM
scm_random (SCM n
, SCM state
);
85 SCM_API SCM
scm_copy_random_state (SCM state
);
86 SCM_API SCM
scm_seed_to_random_state (SCM seed
);
87 SCM_API SCM
scm_datum_to_random_state (SCM datum
);
88 SCM_API SCM
scm_random_state_to_datum (SCM state
);
89 SCM_API SCM
scm_random_state_from_platform (void);
90 SCM_API SCM
scm_random_uniform (SCM state
);
91 SCM_API SCM
scm_random_solid_sphere_x (SCM v
, SCM state
);
92 SCM_API SCM
scm_random_hollow_sphere_x (SCM v
, SCM state
);
93 SCM_API SCM
scm_random_normal (SCM state
);
94 SCM_API SCM
scm_random_normal_vector_x (SCM v
, SCM state
);
95 SCM_API SCM
scm_random_exp (SCM state
);
96 SCM_INTERNAL
void scm_init_random (void);
98 SCM_INTERNAL
void scm_i_random_bytes_from_platform (unsigned char *buf
, size_t len
);
100 #endif /* SCM_RANDOM_H */