From: Francogrex
Subject: *random-state*
Date: 
Message-ID: <af7e479f-ddbc-42b3-abda-f40a171bb8bd@j12g2000vbl.googlegroups.com>
I never quite understood that in CL: For example in SBCL, when I start
and type (random 1.0) I get a certain result say 0.5099. Then I quit
and restart SBCL again and (random 1.0) is again the same number
0.5099 (however if the session is still open (random 1.0) will change
everytime). But in ECL, upon each load (random 1.0) is a new value. In
R I used to work with seeds, where once you set the seed, random would
always give the same numbers (even within a same session). How to set
the seed in CL so that (random 1.0) gives always the same value (even
within the same session)? and conversly how to modify the seed
everytime to control the output of random (sort of control)? Thanks

From: Robert Uhl
Subject: Re: *random-state*
Date: 
Message-ID: <m3iqip4695.fsf@latakia.octopodial-chrome.com>
Francogrex <······@grex.org> writes:

> I never quite understood that in CL: For example in SBCL, when I start
> and type (random 1.0) I get a certain result say 0.5099. Then I quit
> and restart SBCL again and (random 1.0) is again the same number
> 0.5099 (however if the session is still open (random 1.0) will change
> everytime). But in ECL, upon each load (random 1.0) is a new value. In
> R I used to work with seeds, where once you set the seed, random would
> always give the same numbers (even within a same session). How to set
> the seed in CL so that (random 1.0) gives always the same value (even
> within the same session)? and conversly how to modify the seed
> everytime to control the output of random (sort of control)? Thanks

You can get the current random state with '(make-random-state nil)',
save it and then call RANDOM; if you reload the previous random state by
setting *RANDOM-STATE* to it and call RANDOM again then you'll be good.

You don't really get a whole lot of control over the seed; you can't,
for example, portably set the current seed to 194,345,948.  But you can
get a 'truly' random state with '(make-random-state t)' and set
*RANDOM-STATE* to it if you want.

Be aware that some implementations (e.g. SBCL) only generate one 'truly'
random state per second...so if you call MAKE-RANDOM-STATE a dozen times
you'll get a dozen copies of the same seed.

-- 
Robert Uhl <http://public.xdi.org/=ruhl>
The trouble with things that extend your lifespan is that they happen at
the wrong end.  I'd hate to be wearing Depends at 85 and thinking `I
gave up booze for three more years of this.'          --Peter Coffin
From: D Herring
Subject: Re: *random-state*
Date: 
Message-ID: <4a3eab4c$0$29138$6e1ede2f@read.cnntp.org>
Francogrex wrote:
> I never quite understood that in CL: For example in SBCL, when I start
> and type (random 1.0) I get a certain result say 0.5099. Then I quit
> and restart SBCL again and (random 1.0) is again the same number
> 0.5099 (however if the session is still open (random 1.0) will change
> everytime). But in ECL, upon each load (random 1.0) is a new value. In
> R I used to work with seeds, where once you set the seed, random would
> always give the same numbers (even within a same session). How to set
> the seed in CL so that (random 1.0) gives always the same value (even
> within the same session)? and conversly how to modify the seed
> everytime to control the output of random (sort of control)? Thanks

Yeah.  Pseudo-random number functionality is largely implementation 
defined.  In other words, it is implemented pseudo-randomly.

Look at
http://www.lispworks.com/documentation/HyperSpec/Body/t_rnd_st.htm#random-state
http://www.lispworks.com/documentation/HyperSpec/Body/f_mk_rnd.htm#make-random-state

It appears to me that the only portable way to get a consistent value 
between runs is to print out *random-state* somewhere and read it back 
in later.

For more control, you'll want to use a random-number library.  Several 
can be found by searching on cliki.
http://www.cliki.net/Mersenne_Twister
http://www.cliki.net/cl-randist
http://common-lisp.net/project/cl-variates/
...


- Daniel
From: Pascal J. Bourguignon
Subject: Re: *random-state*
Date: 
Message-ID: <87my8145ak.fsf@galatea.local>
Francogrex <······@grex.org> writes:

> I never quite understood that in CL: For example in SBCL, when I start
> and type (random 1.0) I get a certain result say 0.5099. Then I quit
> and restart SBCL again and (random 1.0) is again the same number
> 0.5099 (however if the session is still open (random 1.0) will change
> everytime). But in ECL, upon each load (random 1.0) is a new value. In
> R I used to work with seeds, where once you set the seed, random would
> always give the same numbers (even within a same session). How to set
> the seed in CL so that (random 1.0) gives always the same value (even
> within the same session)? and conversly how to modify the seed
> everytime to control the output of random (sort of control)? Thanks

(defun always-the-same ()
   (let ((*random-state* (make-random-state nil))) (random 1.0)))

(defvar *initialized* nil)
(defun always-different ()
  (unless *initialized* 
     (setf *random-state* (make-random-state t)))
  (random 1.0))

Or said otherwise, read clhs *RANDOM-STATE* and clhs MAKE-RANDOM-STATE !

-- 
__Pascal Bourguignon__