1 /* Copyright (C) 1999,2000,2001, 2003, 2005, 2006, 2009, 2010 Free Software Foundation, Inc.
2 * This library is free software; you can redistribute it and/or
3 * modify it under the terms of the GNU Lesser General Public License
4 * as published by the Free Software Foundation; either version 3 of
5 * the License, or (at your option) any later version.
7 * This library is distributed in the hope that it will be useful, but
8 * WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
10 * Lesser General Public License for more details.
12 * You should have received a copy of the GNU Lesser General Public
13 * License along with this library; if not, write to the Free Software
14 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
20 /* Author: Mikael Djurfeldt <djurfeldt@nada.kth.se> */
26 #include "libguile/_scm.h"
32 #include "libguile/smob.h"
33 #include "libguile/numbers.h"
34 #include "libguile/feature.h"
35 #include "libguile/strings.h"
36 #include "libguile/arrays.h"
37 #include "libguile/srfi-4.h"
38 #include "libguile/vectors.h"
39 #include "libguile/generalized-vectors.h"
41 #include "libguile/validate.h"
42 #include "libguile/random.h"
46 * A plugin interface for RNGs
48 * Using this interface, it is possible for the application to tell
49 * libguile to use a different RNG. This is desirable if it is
50 * necessary to use the same RNG everywhere in the application in
51 * order to prevent interference, if the application uses RNG
52 * hardware, or if the application has special demands on the RNG.
54 * Look in random.h and how the default generator is "plugged in" in
58 scm_t_rng scm_the_rng
;
64 * This is the MWC (Multiply With Carry) random number generator
65 * described by George Marsaglia at the Department of Statistics and
66 * Supercomputer Computations Research Institute, The Florida State
67 * University (http://stat.fsu.edu/~geo).
69 * It uses 64 bits, has a period of 4578426017172946943 (4.6e18), and
70 * passes all tests in the DIEHARD test suite
71 * (http://stat.fsu.edu/~geo/diehard.html)
74 typedef struct scm_t_i_rstate
{
81 #define A 2131995753UL
84 #define M_PI 3.14159265359
88 scm_i_uniform32 (scm_t_rstate
*state
)
90 scm_t_i_rstate
*istate
= (scm_t_i_rstate
*) state
;
91 scm_t_uint64 x
= (scm_t_uint64
) A
* istate
->w
+ istate
->c
;
92 scm_t_uint32 w
= x
& 0xffffffffUL
;
99 scm_i_init_rstate (scm_t_rstate
*state
, const char *seed
, int n
)
101 scm_t_i_rstate
*istate
= (scm_t_i_rstate
*) state
;
105 for (i
= 0; i
< n
; ++i
)
109 w
+= seed
[i
] << (8 * m
);
111 c
+= seed
[i
] << (8 * (m
- 4));
113 if ((w
== 0 && c
== 0) || (w
== -1 && c
== A
- 1))
119 static scm_t_rstate
*
120 scm_i_copy_rstate (scm_t_rstate
*state
)
122 scm_t_rstate
*new_state
;
124 new_state
= scm_gc_malloc_pointerless (state
->rng
->rstate_size
,
126 return memcpy (new_state
, state
, state
->rng
->rstate_size
);
129 SCM_SYMBOL(scm_i_rstate_tag
, "multiply-with-carry");
132 scm_i_rstate_from_datum (scm_t_rstate
*state
, SCM value
)
133 #define FUNC_NAME "scm_i_rstate_from_datum"
135 scm_t_i_rstate
*istate
= (scm_t_i_rstate
*) state
;
139 SCM_VALIDATE_LIST_COPYLEN (SCM_ARG1
, value
, length
);
140 SCM_ASSERT (length
== 3, value
, SCM_ARG1
, FUNC_NAME
);
141 SCM_ASSERT (scm_is_eq (SCM_CAR (value
), scm_i_rstate_tag
),
142 value
, SCM_ARG1
, FUNC_NAME
);
143 SCM_VALIDATE_UINT_COPY (SCM_ARG1
, SCM_CADR (value
), w
);
144 SCM_VALIDATE_UINT_COPY (SCM_ARG1
, SCM_CADDR (value
), c
);
152 scm_i_rstate_to_datum (scm_t_rstate
*state
)
154 scm_t_i_rstate
*istate
= (scm_t_i_rstate
*) state
;
155 return scm_list_3 (scm_i_rstate_tag
,
156 scm_from_uint32 (istate
->w
),
157 scm_from_uint32 (istate
->c
));
162 * Random number library functions
166 scm_c_make_rstate (const char *seed
, int n
)
170 state
= scm_gc_malloc_pointerless (scm_the_rng
.rstate_size
,
172 state
->rng
= &scm_the_rng
;
173 state
->normal_next
= 0.0;
174 state
->rng
->init_rstate (state
, seed
, n
);
179 scm_c_rstate_from_datum (SCM datum
)
183 state
= scm_gc_malloc_pointerless (scm_the_rng
.rstate_size
,
185 state
->rng
= &scm_the_rng
;
186 state
->normal_next
= 0.0;
187 state
->rng
->from_datum (state
, datum
);
192 scm_c_default_rstate ()
193 #define FUNC_NAME "scm_c_default_rstate"
195 SCM state
= SCM_VARIABLE_REF (scm_var_random_state
);
196 if (!SCM_RSTATEP (state
))
197 SCM_MISC_ERROR ("*random-state* contains bogus random state", SCM_EOL
);
198 return SCM_RSTATE (state
);
204 scm_c_uniform01 (scm_t_rstate
*state
)
206 double x
= (double) state
->rng
->random_bits (state
) / (double) 0xffffffffUL
;
207 return ((x
+ (double) state
->rng
->random_bits (state
))
208 / (double) 0xffffffffUL
);
212 scm_c_normal01 (scm_t_rstate
*state
)
214 if (state
->normal_next
!= 0.0)
216 double ret
= state
->normal_next
;
218 state
->normal_next
= 0.0;
226 r
= sqrt (-2.0 * log (scm_c_uniform01 (state
)));
227 a
= 2.0 * M_PI
* scm_c_uniform01 (state
);
230 state
->normal_next
= r
* cos (a
);
237 scm_c_exp1 (scm_t_rstate
*state
)
239 return - log (scm_c_uniform01 (state
));
242 unsigned char scm_masktab
[256];
244 static inline scm_t_uint32
245 scm_i_mask32 (scm_t_uint32 m
)
250 ? scm_masktab
[m
>> 8] << 8 | 0xff
252 ? scm_masktab
[m
>> 16] << 16 | 0xffff
253 : scm_masktab
[m
>> 24] << 24 | 0xffffff)));
257 scm_c_random (scm_t_rstate
*state
, scm_t_uint32 m
)
259 scm_t_uint32 r
, mask
= scm_i_mask32 (m
);
260 while ((r
= state
->rng
->random_bits (state
) & mask
) >= m
);
265 scm_c_random64 (scm_t_rstate
*state
, scm_t_uint64 m
)
270 if (m
<= SCM_T_UINT32_MAX
)
271 return scm_c_random (state
, (scm_t_uint32
) m
);
273 mask
= scm_i_mask32 (m
>> 32);
274 while ((r
= ((scm_t_uint64
) (state
->rng
->random_bits (state
) & mask
) << 32)
275 | state
->rng
->random_bits (state
)) >= m
)
281 SCM scm_c_random_bignum (scm_t_rstate *state, SCM m)
283 Takes a random state (source of random bits) and a bignum m.
284 Returns a bignum b, 0 <= b < m.
286 It does this by allocating a bignum b with as many base 65536 digits
287 as m, filling b with random bits (in 32 bit chunks) up to the most
288 significant 1 in m, and, finally checking if the resultant b is too
289 large (>= m). If too large, we simply repeat the process again. (It
290 is important to throw away all generated random bits if b >= m,
291 otherwise we'll end up with a distorted distribution.)
296 scm_c_random_bignum (scm_t_rstate
*state
, SCM m
)
298 SCM result
= scm_i_mkbig ();
299 const size_t m_bits
= mpz_sizeinbase (SCM_I_BIG_MPZ (m
), 2);
300 /* how many bits would only partially fill the last scm_t_uint32? */
301 const size_t end_bits
= m_bits
% (sizeof (scm_t_uint32
) * SCM_CHAR_BIT
);
302 scm_t_uint32
*random_chunks
= NULL
;
303 const scm_t_uint32 num_full_chunks
=
304 m_bits
/ (sizeof (scm_t_uint32
) * SCM_CHAR_BIT
);
305 const scm_t_uint32 num_chunks
= num_full_chunks
+ ((end_bits
) ? 1 : 0);
307 /* we know the result will be this big */
308 mpz_realloc2 (SCM_I_BIG_MPZ (result
), m_bits
);
311 (scm_t_uint32
*) scm_gc_calloc (num_chunks
* sizeof (scm_t_uint32
),
312 "random bignum chunks");
316 scm_t_uint32
*current_chunk
= random_chunks
+ (num_chunks
- 1);
317 scm_t_uint32 chunks_left
= num_chunks
;
319 mpz_set_ui (SCM_I_BIG_MPZ (result
), 0);
323 /* generate a mask with ones in the end_bits position, i.e. if
324 end_bits is 3, then we'd have a mask of ...0000000111 */
325 const scm_t_uint32 rndbits
= state
->rng
->random_bits (state
);
326 int rshift
= (sizeof (scm_t_uint32
) * SCM_CHAR_BIT
) - end_bits
;
327 scm_t_uint32 mask
= ((scm_t_uint32
)-1) >> rshift
;
328 scm_t_uint32 highest_bits
= rndbits
& mask
;
329 *current_chunk
-- = highest_bits
;
335 /* now fill in the remaining scm_t_uint32 sized chunks */
336 *current_chunk
-- = state
->rng
->random_bits (state
);
339 mpz_import (SCM_I_BIG_MPZ (result
),
342 sizeof (scm_t_uint32
),
346 /* if result >= m, regenerate it (it is important to regenerate
347 all bits in order not to get a distorted distribution) */
348 } while (mpz_cmp (SCM_I_BIG_MPZ (result
), SCM_I_BIG_MPZ (m
)) >= 0);
349 scm_gc_free (random_chunks
,
350 num_chunks
* sizeof (scm_t_uint32
),
351 "random bignum chunks");
352 return scm_i_normbig (result
);
356 * Scheme level representation of random states.
359 scm_t_bits scm_tc16_rstate
;
362 make_rstate (scm_t_rstate
*state
)
364 SCM_RETURN_NEWSMOB (scm_tc16_rstate
, state
);
369 * Scheme level interface.
372 SCM_GLOBAL_VARIABLE_INIT (scm_var_random_state
, "*random-state*", scm_seed_to_random_state (scm_from_locale_string ("URL:http://stat.fsu.edu/~geo/diehard.html")));
374 SCM_DEFINE (scm_random
, "random", 1, 1, 0,
376 "Return a number in [0, N).\n"
378 "Accepts a positive integer or real n and returns a\n"
379 "number of the same type between zero (inclusive) and\n"
380 "N (exclusive). The values returned have a uniform\n"
383 "The optional argument @var{state} must be of the type produced\n"
384 "by @code{seed->random-state}. It defaults to the value of the\n"
385 "variable @var{*random-state*}. This object is used to maintain\n"
386 "the state of the pseudo-random-number generator and is altered\n"
387 "as a side effect of the random operation.")
388 #define FUNC_NAME s_scm_random
390 if (SCM_UNBNDP (state
))
391 state
= SCM_VARIABLE_REF (scm_var_random_state
);
392 SCM_VALIDATE_RSTATE (2, state
);
395 scm_t_bits m
= (scm_t_bits
) SCM_I_INUM (n
);
396 SCM_ASSERT_RANGE (1, n
, SCM_I_INUM (n
) > 0);
397 #if SCM_SIZEOF_UINTPTR_T <= 4
398 return scm_from_uint32 (scm_c_random (SCM_RSTATE (state
),
400 #elif SCM_SIZEOF_UINTPTR_T <= 8
401 return scm_from_uint64 (scm_c_random64 (SCM_RSTATE (state
),
404 #error "Cannot deal with this platform's scm_t_bits size"
407 SCM_VALIDATE_NIM (1, n
);
409 return scm_from_double (SCM_REAL_VALUE (n
)
410 * scm_c_uniform01 (SCM_RSTATE (state
)));
413 SCM_WRONG_TYPE_ARG (1, n
);
414 return scm_c_random_bignum (SCM_RSTATE (state
), n
);
418 SCM_DEFINE (scm_copy_random_state
, "copy-random-state", 0, 1, 0,
420 "Return a copy of the random state @var{state}.")
421 #define FUNC_NAME s_scm_copy_random_state
423 if (SCM_UNBNDP (state
))
424 state
= SCM_VARIABLE_REF (scm_var_random_state
);
425 SCM_VALIDATE_RSTATE (1, state
);
426 return make_rstate (SCM_RSTATE (state
)->rng
->copy_rstate (SCM_RSTATE (state
)));
430 SCM_DEFINE (scm_seed_to_random_state
, "seed->random-state", 1, 0, 0,
432 "Return a new random state using @var{seed}.")
433 #define FUNC_NAME s_scm_seed_to_random_state
436 if (SCM_NUMBERP (seed
))
437 seed
= scm_number_to_string (seed
, SCM_UNDEFINED
);
438 SCM_VALIDATE_STRING (1, seed
);
439 res
= make_rstate (scm_c_make_rstate (scm_i_string_chars (seed
),
440 scm_i_string_length (seed
)));
441 scm_remember_upto_here_1 (seed
);
447 SCM_DEFINE (scm_datum_to_random_state
, "datum->random-state", 1, 0, 0,
449 "Return a new random state using @var{datum}, which should have\n"
450 "been obtained from @code{random-state->datum}.")
451 #define FUNC_NAME s_scm_datum_to_random_state
453 return make_rstate (scm_c_rstate_from_datum (datum
));
457 SCM_DEFINE (scm_random_state_to_datum
, "random-state->datum", 1, 0, 0,
459 "Return a datum representation of @var{state} that may be\n"
460 "written out and read back with the Scheme reader.")
461 #define FUNC_NAME s_scm_random_state_to_datum
463 SCM_VALIDATE_RSTATE (1, state
);
464 return SCM_RSTATE (state
)->rng
->to_datum (SCM_RSTATE (state
));
468 SCM_DEFINE (scm_random_uniform
, "random:uniform", 0, 1, 0,
470 "Return a uniformly distributed inexact real random number in\n"
472 #define FUNC_NAME s_scm_random_uniform
474 if (SCM_UNBNDP (state
))
475 state
= SCM_VARIABLE_REF (scm_var_random_state
);
476 SCM_VALIDATE_RSTATE (1, state
);
477 return scm_from_double (scm_c_uniform01 (SCM_RSTATE (state
)));
481 SCM_DEFINE (scm_random_normal
, "random:normal", 0, 1, 0,
483 "Return an inexact real in a normal distribution. The\n"
484 "distribution used has mean 0 and standard deviation 1. For a\n"
485 "normal distribution with mean m and standard deviation d use\n"
486 "@code{(+ m (* d (random:normal)))}.")
487 #define FUNC_NAME s_scm_random_normal
489 if (SCM_UNBNDP (state
))
490 state
= SCM_VARIABLE_REF (scm_var_random_state
);
491 SCM_VALIDATE_RSTATE (1, state
);
492 return scm_from_double (scm_c_normal01 (SCM_RSTATE (state
)));
497 vector_scale_x (SCM v
, double c
)
500 if (scm_is_simple_vector (v
))
502 n
= SCM_SIMPLE_VECTOR_LENGTH (v
);
504 SCM_REAL_VALUE (SCM_SIMPLE_VECTOR_REF (v
, n
)) *= c
;
508 /* must be a f64vector. */
509 scm_t_array_handle handle
;
514 elts
= scm_f64vector_writable_elements (v
, &handle
, &len
, &inc
);
516 for (i
= 0; i
< len
; i
++, elts
+= inc
)
519 scm_array_handle_release (&handle
);
524 vector_sum_squares (SCM v
)
528 if (scm_is_simple_vector (v
))
530 n
= SCM_SIMPLE_VECTOR_LENGTH (v
);
533 x
= SCM_REAL_VALUE (SCM_SIMPLE_VECTOR_REF (v
, n
));
539 /* must be a f64vector. */
540 scm_t_array_handle handle
;
545 elts
= scm_f64vector_elements (v
, &handle
, &len
, &inc
);
547 for (i
= 0; i
< len
; i
++, elts
+= inc
)
553 scm_array_handle_release (&handle
);
558 /* For the uniform distribution on the solid sphere, note that in
559 * this distribution the length r of the vector has cumulative
560 * distribution r^n; i.e., u=r^n is uniform [0,1], so r can be
561 * generated as r=u^(1/n).
563 SCM_DEFINE (scm_random_solid_sphere_x
, "random:solid-sphere!", 1, 1, 0,
565 "Fills @var{vect} with inexact real random numbers the sum of\n"
566 "whose squares is less than 1.0. Thinking of @var{vect} as\n"
567 "coordinates in space of dimension @var{n} @math{=}\n"
568 "@code{(vector-length @var{vect})}, the coordinates are\n"
569 "uniformly distributed within the unit @var{n}-sphere.")
570 #define FUNC_NAME s_scm_random_solid_sphere_x
572 if (SCM_UNBNDP (state
))
573 state
= SCM_VARIABLE_REF (scm_var_random_state
);
574 SCM_VALIDATE_RSTATE (2, state
);
575 scm_random_normal_vector_x (v
, state
);
577 pow (scm_c_uniform01 (SCM_RSTATE (state
)),
578 1.0 / scm_c_generalized_vector_length (v
))
579 / sqrt (vector_sum_squares (v
)));
580 return SCM_UNSPECIFIED
;
584 SCM_DEFINE (scm_random_hollow_sphere_x
, "random:hollow-sphere!", 1, 1, 0,
586 "Fills vect with inexact real random numbers\n"
587 "the sum of whose squares is equal to 1.0.\n"
588 "Thinking of vect as coordinates in space of\n"
589 "dimension n = (vector-length vect), the coordinates\n"
590 "are uniformly distributed over the surface of the\n"
592 #define FUNC_NAME s_scm_random_hollow_sphere_x
594 if (SCM_UNBNDP (state
))
595 state
= SCM_VARIABLE_REF (scm_var_random_state
);
596 SCM_VALIDATE_RSTATE (2, state
);
597 scm_random_normal_vector_x (v
, state
);
598 vector_scale_x (v
, 1 / sqrt (vector_sum_squares (v
)));
599 return SCM_UNSPECIFIED
;
604 SCM_DEFINE (scm_random_normal_vector_x
, "random:normal-vector!", 1, 1, 0,
606 "Fills vect with inexact real random numbers that are\n"
607 "independent and standard normally distributed\n"
608 "(i.e., with mean 0 and variance 1).")
609 #define FUNC_NAME s_scm_random_normal_vector_x
612 scm_t_array_handle handle
;
613 scm_t_array_dim
*dim
;
615 if (SCM_UNBNDP (state
))
616 state
= SCM_VARIABLE_REF (scm_var_random_state
);
617 SCM_VALIDATE_RSTATE (2, state
);
619 scm_generalized_vector_get_handle (v
, &handle
);
620 dim
= scm_array_handle_dims (&handle
);
622 if (scm_is_vector (v
))
624 SCM
*elts
= scm_array_handle_writable_elements (&handle
);
625 for (i
= dim
->lbnd
; i
<= dim
->ubnd
; i
++, elts
+= dim
->inc
)
626 *elts
= scm_from_double (scm_c_normal01 (SCM_RSTATE (state
)));
630 /* must be a f64vector. */
631 double *elts
= scm_array_handle_f64_writable_elements (&handle
);
632 for (i
= dim
->lbnd
; i
<= dim
->ubnd
; i
++, elts
+= dim
->inc
)
633 *elts
= scm_c_normal01 (SCM_RSTATE (state
));
636 scm_array_handle_release (&handle
);
638 return SCM_UNSPECIFIED
;
642 SCM_DEFINE (scm_random_exp
, "random:exp", 0, 1, 0,
644 "Return an inexact real in an exponential distribution with mean\n"
645 "1. For an exponential distribution with mean u use (* u\n"
647 #define FUNC_NAME s_scm_random_exp
649 if (SCM_UNBNDP (state
))
650 state
= SCM_VARIABLE_REF (scm_var_random_state
);
651 SCM_VALIDATE_RSTATE (1, state
);
652 return scm_from_double (scm_c_exp1 (SCM_RSTATE (state
)));
656 /* Return a new random-state seeded from the time, date, process ID, an
657 address from a freshly allocated heap cell, an address from the local
658 stack frame, and a high-resolution timer if available. This is only
659 to be used as a last resort, when no better source of entropy is
662 random_state_of_last_resort (void)
665 SCM time_of_day
= scm_gettimeofday ();
666 SCM sources
= scm_list_n
667 (scm_from_unsigned_integer (SCM_UNPACK (time_of_day
)), /* heap addr */
668 scm_getpid (), /* process ID */
669 scm_get_internal_real_time (), /* high-resolution process timer */
670 scm_from_unsigned_integer ((scm_t_bits
) &time_of_day
), /* stack addr */
671 scm_car (time_of_day
), /* seconds since midnight 1970-01-01 UTC */
672 scm_cdr (time_of_day
), /* microsecond component of the above clock */
675 /* Concatenate the sources bitwise to form the seed */
676 SCM seed
= SCM_INUM0
;
677 while (scm_is_pair (sources
))
679 seed
= scm_logxor (seed
, scm_ash (scm_car (sources
),
680 scm_integer_length (seed
)));
681 sources
= scm_cdr (sources
);
684 /* FIXME The following code belongs in `scm_seed_to_random_state',
685 and here we should simply do:
687 return scm_seed_to_random_state (seed);
689 Unfortunately, `scm_seed_to_random_state' only preserves around 32
690 bits of entropy from the provided seed. I don't know if it's okay
691 to fix that in 2.0, so for now we have this workaround. */
695 len
= scm_to_int (scm_ceiling_quotient (scm_integer_length (seed
),
697 buf
= (unsigned char *) malloc (len
);
698 for (i
= len
-1; i
>= 0; --i
)
700 buf
[i
] = scm_to_int (scm_logand (seed
, SCM_I_MAKINUM (255)));
701 seed
= scm_ash (seed
, SCM_I_MAKINUM (-8));
703 state
= make_rstate (scm_c_make_rstate ((char *) buf
, len
));
709 /* Attempt to fill buffer with random bytes from /dev/urandom.
710 Return 1 if successful, else return 0. */
712 read_dev_urandom (unsigned char *buf
, size_t len
)
715 FILE *f
= fopen ("/dev/urandom", "r");
718 res
= fread(buf
, 1, len
, f
);
724 /* Fill a buffer with random bytes seeded from a platform-specific
725 source of entropy. /dev/urandom is used if available. Note that
726 this function provides no guarantees about the amount of entropy
727 present in the returned bytes. */
729 scm_i_random_bytes_from_platform (unsigned char *buf
, size_t len
)
731 if (read_dev_urandom (buf
, len
))
733 else /* FIXME: support other platform sources */
735 /* When all else fails, use this (rather weak) fallback */
736 SCM random_state
= random_state_of_last_resort ();
738 for (i
= len
-1; i
>= 0; --i
)
739 buf
[i
] = scm_to_int (scm_random (SCM_I_MAKINUM (256), random_state
));
743 SCM_DEFINE (scm_random_state_from_platform
, "random-state-from-platform", 0, 0, 0,
745 "Construct a new random state seeded from a platform-specific\n\
746 source of entropy, appropriate for use in non-security-critical applications.")
747 #define FUNC_NAME s_scm_random_state_from_platform
749 unsigned char buf
[32];
750 if (read_dev_urandom (buf
, sizeof(buf
)))
751 return make_rstate (scm_c_make_rstate ((char *) buf
, sizeof(buf
)));
753 return random_state_of_last_resort ();
761 /* plug in default RNG */
764 sizeof (scm_t_i_rstate
),
768 scm_i_rstate_from_datum
,
769 scm_i_rstate_to_datum
773 scm_tc16_rstate
= scm_make_smob_type ("random-state", 0);
775 for (m
= 1; m
<= 0x100; m
<<= 1)
776 for (i
= m
>> 1; i
< m
; ++i
)
777 scm_masktab
[i
] = m
- 1;
779 #include "libguile/random.x"
781 scm_add_feature ("random");