1 /* Copyright (C) 1999 Free Software Foundation, Inc.
2 * This program is free software; you can redistribute it and/or modify
3 * it under the terms of the GNU General Public License as published by
4 * the Free Software Foundation; either version 2, or (at your option)
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
12 * You should have received a copy of the GNU General Public License
13 * along with this software; see the file COPYING. If not, write to
14 * the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
15 * Boston, MA 02111-1307 USA
17 * As a special exception, the Free Software Foundation gives permission
18 * for additional uses of the text contained in its release of GUILE.
20 * The exception is that, if you link the GUILE library with other files
21 * to produce an executable, this does not by itself cause the
22 * resulting executable to be covered by the GNU General Public License.
23 * Your use of that executable is in no way restricted on account of
24 * linking the GUILE library code into it.
26 * This exception does not however invalidate any other reasons why
27 * the executable file might be covered by the GNU General Public License.
29 * This exception applies only to the code released by the
30 * Free Software Foundation under the name GUILE. If you copy
31 * code from other Free Software Foundation releases into a copy of
32 * GUILE, as the General Public License permits, the exception does
33 * not apply to the code that you add in this way. To avoid misleading
34 * anyone as to the status of such modified files, you must delete
35 * this exception notice from them.
37 * If you write modifications of your own for GUILE, it is your choice
38 * whether to permit this exception to apply to your modifications.
39 * If you do not wish that, delete this exception notice. */
41 /* Author: Mikael Djurfeldt <djurfeldt@nada.kth.se> */
55 * A plugin interface for RNGs
57 * Using this interface, it is possible for the application to tell
58 * libguile to use a different RNG. This is desirable if it is
59 * necessary to use the same RNG everywhere in the application in
60 * order to prevent interference, if the application uses RNG
61 * hardware, or if the application has special demands on the RNG.
63 * Look in random.h and how the default generator is "plugged in" in
73 * This is the MWC (Multiply With Carry) random number generator
74 * described by George Marsaglia at the Department of Statistics and
75 * Supercomputer Computations Research Institute, The Florida State
76 * University (http://stat.fsu.edu/~geo).
78 * It uses 64 bits, has a period of 4578426017172946943 (4.6e18), and
79 * passes all tests in the DIEHARD test suite
80 * (http://stat.fsu.edu/~geo/diehard.html)
83 #define A 2131995753UL
87 #define LONG32 unsigned short
89 #define LONG32 unsigned int
91 #define LONG64 unsigned long
93 #define LONG32 unsigned long
94 #define LONG64 unsigned long long
97 #if SIZEOF_LONG > 4 || defined (HAVE_LONG_LONGS)
100 scm_i_uniform32 (scm_i_rstate
*state
)
102 LONG64 x
= (LONG64
) A
* state
->w
+ state
->c
;
103 LONG32 w
= x
& 0xffffffffUL
;
111 /* ww This is a portable version of the same RNG without 64 bit
114 * xx It is only intended to provide identical behaviour on
115 * xx platforms without 8 byte longs or long longs until
116 * xx someone has implemented the routine in assembler code.
122 #define L(x) ((x) & 0xffff)
123 #define H(x) ((x) >> 16)
126 scm_i_uniform32 (scm_i_rstate
*state
)
128 LONG32 x1
= L (A
) * L (state
->w
);
129 LONG32 x2
= L (A
) * H (state
->w
);
130 LONG32 x3
= H (A
) * L (state
->w
);
131 LONG32 w
= L (x1
) + L (state
->c
);
132 LONG32 m
= H (x1
) + L (x2
) + L (x3
) + H (state
->c
) + H (w
);
133 LONG32 x4
= H (A
) * H (state
->w
);
134 state
->w
= w
= (L (m
) << 16) + L (w
);
135 state
->c
= H (x2
) + H (x3
) + x4
+ H (m
);
142 scm_i_init_rstate (scm_i_rstate
*state
, char *seed
, int n
)
147 for (i
= 0; i
< n
; ++i
)
151 w
+= seed
[i
] << (8 * m
);
153 c
+= seed
[i
] << (8 * (m
- 4));
155 if ((w
== 0 && c
== 0) || (w
== 0xffffffffUL
&& c
== A
- 1))
162 scm_i_copy_rstate (scm_i_rstate
*state
)
164 scm_rstate
*new_state
= malloc (scm_the_rng
.rstate_size
);
166 scm_wta (SCM_MAKINUM (scm_the_rng
.rstate_size
),
167 (char *) SCM_NALLOC
, "rstate");
168 return memcpy (new_state
, state
, scm_the_rng
.rstate_size
);
173 * Random number library functions
177 scm_i_uniform01 (scm_rstate
*state
)
179 double x
= (double) scm_the_rng
.random_bits (state
) / (double) 0xffffffffUL
;
180 return ((x
+ (double) scm_the_rng
.random_bits (state
))
181 / (double) 0xffffffffUL
);
185 scm_i_normal01 (scm_rstate
*state
)
187 if (state
->reserved0
)
189 state
->reserved0
= 0;
190 return state
->reserved1
;
196 r
= sqrt (-2.0 * log (scm_i_uniform01 (state
)));
197 a
= 2.0 * M_PI
* scm_i_uniform01 (state
);
200 state
->reserved1
= r
* cos (a
);
201 state
->reserved0
= 1;
208 scm_i_exp1 (scm_rstate
*state
)
210 return - log (scm_i_uniform01 (state
));
213 unsigned char scm_masktab
[256];
216 scm_i_random (unsigned long m
, scm_rstate
*state
)
218 unsigned int r
, mask
;
222 ? scm_masktab
[m
>> 8] << 8 | 0xff
224 ? scm_masktab
[m
>> 16] << 16 | 0xffff
225 : scm_masktab
[m
>> 24] << 24 | 0xffffff)));
226 while ((r
= scm_the_rng
.random_bits (state
) & mask
) >= m
);
231 scm_i_random_bignum (SCM m
, scm_rstate
*state
)
235 LONG32
*bits
, mask
, w
;
236 nd
= SCM_NUMDIGS (m
);
237 /* calculate mask for most significant digit */
242 /* fix most significant 16 bits */
243 unsigned short s
= SCM_BDIGITS (m
)[nd
- 1];
244 mask
= s
< 0x100 ? scm_masktab
[s
] : scm_masktab
[s
>> 8] << 8 | 0xff;
249 /* fix most significant 32 bits */
250 #if SIZEOF_INT == 4 && defined (WORDS_BIGENDIAN)
251 w
= SCM_BDIGITS (m
)[nd
- 1] << 16 | SCM_BDIGITS (m
)[nd
- 2];
253 w
= ((LONG32
*) SCM_BDIGITS (m
))[nd
/ 2 - 1];
258 : scm_masktab
[w
>> 8] << 8 | 0xff)
260 ? scm_masktab
[w
>> 16] << 16 | 0xffff
261 : scm_masktab
[w
>> 24] << 24 | 0xffffff));
263 b
= scm_mkbig (nd
, 0);
264 bits
= (LONG32
*) SCM_BDIGITS (b
);
268 /* treat most significant digit specially */
273 ((SCM_BIGDIG
*) bits
)[i
- 1] = scm_the_rng
.random_bits (state
) & mask
;
279 /* fix most significant 32 bits */
281 #if SIZEOF_INT == 4 && defined (WORDS_BIGENDIAN)
282 w
= scm_the_rng
.random_bits (state
) & mask
;
283 bits
[--i
] = (w
& 0xffff) << 16 | w
>> 16;
285 bits
[--i
] = scm_the_rng
.random_bits (state
) & mask
;
288 /* now fill up the rest of the bignum */
290 bits
[--i
] = scm_the_rng
.random_bits (state
);
294 } while (scm_bigcomp (b
, m
) <= 0);
299 * Scheme level representation of random states.
302 long scm_tc16_rstate
;
305 make_rstate (scm_rstate
*state
)
310 SCM_SETCDR (cell
, state
);
311 SCM_SETCAR (cell
, scm_tc16_rstate
);
317 print_rstate (SCM rstate
, SCM port
, scm_print_state
*pstate
)
319 scm_puts ("#<random-state ", port
);
320 scm_intprint ((long) SCM_RSTATE (rstate
), 16, port
);
321 scm_putc ('>', port
);
326 free_rstate (SCM rstate
)
328 free (SCM_RSTATE (rstate
));
329 return scm_the_rng
.rstate_size
;
332 static scm_smobfuns rstate_smob
= { 0, free_rstate
, print_rstate
, 0};
335 * Scheme level interface.
338 SCM_GLOBAL_VCELL_INIT (scm_var_random_state
, "*random-state*", scm_make_random_state (scm_makfrom0str ("URL:http://stat.fsu.edu/~geo/diehard.html")));
340 SCM_PROC (s_random
, "random", 1, 1, 0, scm_random
);
343 scm_random (SCM n
, SCM state
)
345 if (SCM_UNBNDP (state
))
346 state
= SCM_CDR (scm_var_random_state
);
347 SCM_ASSERT (SCM_NIMP (state
) && SCM_RSTATEP (state
),
348 state
, SCM_ARG2
, s_random
);
351 unsigned long m
= SCM_INUM (n
);
352 SCM_ASSERT (m
> 0, n
, SCM_ARG1
, s_random
);
353 return SCM_MAKINUM (scm_i_random (m
, SCM_RSTATE (state
)));
355 SCM_ASSERT (SCM_NIMP (n
), n
, SCM_ARG1
, s_random
);
357 return scm_makdbl (SCM_REALPART (n
) * scm_i_uniform01 (SCM_RSTATE (state
)),
359 SCM_ASSERT (SCM_TYP16 (n
) == scm_tc16_bigpos
, n
, SCM_ARG1
, s_random
);
360 return scm_i_random_bignum (n
, SCM_RSTATE (state
));
363 SCM_PROC (s_make_random_state
, "make-random-state", 0, 1, 0, scm_make_random_state
);
366 scm_make_random_state (SCM state
)
368 if (SCM_UNBNDP (state
))
370 state
= SCM_CDR (scm_var_random_state
);
373 else if (SCM_NUMBERP (state
))
375 state
= scm_number_to_string (state
, SCM_UNDEFINED
);
378 else if (SCM_NIMP (state
) && SCM_STRINGP (state
))
381 scm_rstate
*nstate
= malloc (scm_the_rng
.rstate_size
);
383 scm_wta (SCM_MAKINUM (scm_the_rng
.rstate_size
),
386 nstate
->reserved0
= 0;
387 scm_the_rng
.init_rstate (nstate
, SCM_ROCHARS (state
), SCM_LENGTH (state
));
388 return make_rstate (nstate
);
391 SCM_ASSERT (SCM_NIMP (state
) && SCM_RSTATEP (state
),
394 s_make_random_state
);
395 return make_rstate (scm_the_rng
.copy_rstate (SCM_RSTATE (state
)));
398 SCM_PROC (s_random_uniform
, "random:uniform", 0, 1, 0, scm_random_uniform
);
401 scm_random_uniform (SCM state
)
403 if (SCM_UNBNDP (state
))
404 state
= SCM_CDR (scm_var_random_state
);
405 SCM_ASSERT (SCM_NIMP (state
) && SCM_RSTATEP (state
),
409 return scm_makdbl (scm_i_uniform01 (SCM_RSTATE (state
)), 0.0);
413 vector_scale (SCM v
, double c
)
415 int n
= SCM_LENGTH (v
);
418 SCM_REAL (SCM_VELTS (v
)[n
]) *= c
;
421 ((double *) SCM_VELTS (v
))[n
] *= c
;
425 vector_sum_squares (SCM v
)
428 int n
= SCM_LENGTH (v
);
432 x
= SCM_REAL (SCM_VELTS (v
)[n
]);
438 x
= ((double *) SCM_VELTS (v
))[n
];
444 SCM_PROC (s_random_solid_sphere_x
, "random:solid-sphere!", 1, 1, 0, scm_random_solid_sphere_x
);
446 /* For the uniform distribution on the solid sphere, note that in
447 * this distribution the length r of the vector has cumulative
448 * distribution r^n; i.e., u=r^n is uniform [0,1], so r can be
449 * generated as r=u^(1/n).
452 scm_random_solid_sphere_x (SCM v
, SCM state
)
454 SCM_ASSERT (SCM_NIMP (v
)
455 && (SCM_VECTORP (v
) || SCM_TYP7 (v
) == scm_tc7_dvect
),
456 v
, SCM_ARG1
, s_random_solid_sphere_x
);
457 if (SCM_UNBNDP (state
))
458 state
= SCM_CDR (scm_var_random_state
);
459 SCM_ASSERT (SCM_NIMP (state
) && SCM_RSTATEP (state
),
462 s_random_solid_sphere_x
);
463 scm_random_normal_vector_x (v
, state
);
465 pow (scm_i_uniform01 (SCM_RSTATE (state
)),
466 1.0 / SCM_LENGTH (v
))
467 / sqrt (vector_sum_squares (v
)));
468 return SCM_UNSPECIFIED
;
471 SCM_PROC (s_random_hollow_sphere_x
, "random:hollow-sphere!", 1, 1, 0, scm_random_hollow_sphere_x
);
474 scm_random_hollow_sphere_x (SCM v
, SCM state
)
476 SCM_ASSERT (SCM_NIMP (v
)
477 && (SCM_VECTORP (v
) || SCM_TYP7 (v
) == scm_tc7_dvect
),
478 v
, SCM_ARG1
, s_random_solid_sphere_x
);
479 if (SCM_UNBNDP (state
))
480 state
= SCM_CDR (scm_var_random_state
);
481 SCM_ASSERT (SCM_NIMP (state
) && SCM_RSTATEP (state
),
484 s_random_hollow_sphere_x
);
485 scm_random_normal_vector_x (v
, state
);
486 vector_scale (v
, 1 / sqrt (vector_sum_squares (v
)));
487 return SCM_UNSPECIFIED
;
490 SCM_PROC (s_random_normal
, "random:normal", 0, 1, 0, scm_random_normal
);
493 scm_random_normal (SCM state
)
495 if (SCM_UNBNDP (state
))
496 state
= SCM_CDR (scm_var_random_state
);
497 SCM_ASSERT (SCM_NIMP (state
) && SCM_RSTATEP (state
),
501 return scm_makdbl (scm_i_normal01 (SCM_RSTATE (state
)), 0.0);
504 SCM_PROC (s_random_normal_vector_x
, "random:normal-vector!", 1, 1, 0, scm_random_normal_vector_x
);
507 scm_random_normal_vector_x (SCM v
, SCM state
)
510 SCM_ASSERT (SCM_NIMP (v
)
511 && (SCM_VECTORP (v
) || SCM_TYP7 (v
) == scm_tc7_dvect
),
512 v
, SCM_ARG1
, s_random_solid_sphere_x
);
513 if (SCM_UNBNDP (state
))
514 state
= SCM_CDR (scm_var_random_state
);
515 SCM_ASSERT (SCM_NIMP (state
) && SCM_RSTATEP (state
),
518 s_random_normal_vector_x
);
522 SCM_VELTS (v
)[n
] = scm_makdbl (scm_i_normal01 (SCM_RSTATE (state
)), 0.0);
525 ((double *) SCM_VELTS (v
))[n
] = scm_i_normal01 (SCM_RSTATE (state
));
526 return SCM_UNSPECIFIED
;
529 SCM_PROC (s_random_exp
, "random:exp", 0, 1, 0, scm_random_exp
);
532 scm_random_exp (SCM state
)
534 if (SCM_UNBNDP (state
))
535 state
= SCM_CDR (scm_var_random_state
);
536 SCM_ASSERT (SCM_NIMP (state
) && SCM_RSTATEP (state
),
540 return scm_makdbl (scm_i_exp1 (SCM_RSTATE (state
)), 0.0);
547 /* plug in default RNG */
550 sizeof (scm_i_rstate
),
551 (unsigned long (*)()) scm_i_uniform32
,
552 (void (*)()) scm_i_init_rstate
,
553 (scm_rstate
*(*)()) scm_i_copy_rstate
557 scm_tc16_rstate
= scm_newsmob (&rstate_smob
);
559 for (m
= 1; m
<= 0x100; m
<<= 1)
560 for (i
= m
>> 1; i
< m
; ++i
)
561 scm_masktab
[i
] = m
- 1;
565 /* Check that the assumptions about bits per bignum digit are correct. */
571 if (m
!= SCM_BITSPERDIG
)
573 fprintf (stderr
, "Internal inconsistency: Confused about bignum digit size in random.c\n");
577 scm_add_feature ("random");