From f8bb1dc83786154aa048edb0563d2c2e0806529a Mon Sep 17 00:00:00 2001 From: Matt Birkholz Date: Sat, 8 Feb 2014 13:02:42 -0700 Subject: [PATCH] Fluidize *random-state*. --- doc/ref-manual/numbers.texi | 14 +++++++------- src/runtime/random.scm | 18 +++++++++++------- 2 files changed, 18 insertions(+), 14 deletions(-) diff --git a/doc/ref-manual/numbers.texi b/doc/ref-manual/numbers.texi index 72b047c1b..711fca001 100644 --- a/doc/ref-manual/numbers.texi +++ b/doc/ref-manual/numbers.texi @@ -1364,7 +1364,7 @@ either an exact integer or an inexact real; the current implementation has been tuned to make these two cases fast. If @var{state} is given and not @code{#f}, it must be a random-state -object; otherwise, it defaults to the value of the variable +object; otherwise, it defaults to the value of the fluid @code{*random-state*}. This object is used to maintain the state of the pseudo-random-number generator and is altered as a side effect of the @code{random} procedure. @@ -1399,20 +1399,20 @@ procedure. This allows a particular random-state object to be saved in order to replay a particular pseudo-random sequence. @defvr variable *random-state* -This variable holds a data structure, a random-state object, that +The value of this fluid is a data structure, a random-state object, that encodes the internal state of the random-number generator that @code{random} uses by default. A call to @code{random} will perform a -side effect on this data structure. This variable may be changed, using -@code{set!} or @code{fluid-let}, to hold a new random-state object. +side effect on this data structure. The fluid may be changed, using +@code{set-fluid!} or @code{let-fluid}, to hold a new random-state object. @end defvr @deffn procedure make-random-state [state] This procedure returns a new random-state object, suitable for use as -the value of the variable @code{*random-state*}, or as the @var{state} +the value of the fluid @code{*random-state*}, or as the @var{state} argument to @code{random}. If @var{state} is not given or @code{#f}, @code{make-random-state} returns a @emph{copy} of the current -random-number state object (the value of the variable -@code{*random-state*}). If @var{state} is a random-state object, a copy +random-number state object (the value of the @code{*random-state*} +fluid object). If @var{state} is a random-state object, a copy of that object is returned. If @var{state} is @code{#t}, then a new random-state object is returned that has been ``randomly'' initialized by some means (such as by a time-of-day clock). diff --git a/src/runtime/random.scm b/src/runtime/random.scm index cc4d4584c..6fbcc33f7 100644 --- a/src/runtime/random.scm +++ b/src/runtime/random.scm @@ -373,14 +373,17 @@ USA. (if (not (random-state? state)) (error:wrong-type-argument state "random state" procedure)) state) - (let ((state *random-state*)) + (let ((state (if *random-state* + (fluid *random-state*) + ;; For early in the cold-load... + default-random-source))) (if (not (random-state? state)) (error "Invalid *random-state*:" state)) state))) ;;;; Initialization -(define *random-state*) +(define *random-state* #f) (define flimit.) (define flimit) (define default-random-source) @@ -388,24 +391,25 @@ USA. (define random-real) (define (initialize-package!) - (set! *random-state* (simple-random-state)) (set! flimit. (let loop ((x 1.)) (if (flo:= (flo:+ x 1.) 1.) (flo:/ 1. x) (loop (flo:/ x 2.))))) (set! flimit (flo:truncate->exact flimit.)) - (set! default-random-source *random-state*) + (set! default-random-source (simple-random-state)) (set! random-integer (random-source-make-integers default-random-source)) (set! random-real (random-source-make-reals default-random-source)) unspecific) (define (finalize-random-state-type!) + (set! *random-state* (make-fluid default-random-source)) (add-event-receiver! event:after-restart (lambda () - (random-source-randomize! *random-state*) - (if (not (eq? default-random-source *random-state*)) - (random-source-randomize! default-random-source)))) + (let ((state (fluid *random-state*))) + (random-source-randomize! state) + (if (not (eq? default-random-source state)) + (random-source-randomize! default-random-source))))) (named-structure/set-tag-description! random-state-tag (make-define-structure-type 'VECTOR 'RANDOM-STATE -- 2.25.1