Subject: Re: CCL:Srand and rand

From: [email-masked] (Lorenzo Pueyo)
Date: Fri, 28 Jul 1995 12:40:02 +0200

"G. Ravishanker" <[email-masked]> wrote:
>        I am using the following template in my program (XL Fortran on AIX)
>
>                iseed = 12357
>                call srand(iseed)
>
>                do i=1,n
>                  a(i) = rand()
>                end do
>
>        I have been unable to find a way to extract the random number seed
>at the end, so in case I want to checkpoint my calculation for
>continuation at a later point I can use that seed to reset the random
>number generator. Any ideas on how to get this accomplished? Thanks in
>advance. 

Very simply you can do:

            iseed = 12357
            keepseed = iseed
            call srand(iseed)
             ...

and then use the copy of the initial seed kept in keepseed at any time.
On the other hand, if you are using srand() for any serious purpose
you better invest some time ensuring that the statistical properties
of your generator are random enough for your particular problem.

  Regards, Victor Lua~na

+--------------------------------------------+  +---^---/    /
!               Victor Lua~na                !  |   ~       / Just in case
! Departamento de Quimica Fisica y Analitica !  |           | you don't
! Universidad de Oviedo, 33006-Oviedo, Spain ! <            / remember
!  e-mail: [email-masked]   !  |          /  where Oviedo
!          phone: (34)-8-5103523             !  |____  ___/   is  ;-)
!           fax: (34)-8-5103480              !       \/
+--------------------------------------------+

---

From: [email-masked] (Eric Bittner)
Date: Fri, 28 Jul 1995 09:57:30 -0600

I use the same sequence to generate random numbers.  
To reseed the same sequence, I just recycle  the same large integer
I fed to srand in the first place.

Cheers!
Eric Bittner
-------------------------------------------------------------------
Dr. Eric R. Bittner
Dept of Chemistry
Univ. of Texas at Austin
[email-masked] 
http://eiger.cm.utexas.edu

---

From: [email-masked] (Peter Shenkin)
Date: Fri, 28 Jul 95 10:45:01 -0400

> From: "G. Ravishanker" <[email-masked]>
> number generator....

One response that appeared was to save the initial seed and reuse it,
but that response seemed to misunderstand the question, which is, I
believe, how to *continue* the same random-number sequence -- not
how to reproduce it from the beginning.  I think what Ganesan wants
to do is to be able to perform run1, then run2, and have the trajectory
be the same as if he had performed a single run with the combined length
of run1 and run2.

rand() is supposed to be linear-congruential, which means (among other
things) that any number in the sequence depends only on the previous
number.  Thus, my first guess was that the last integer generated
would be the seed for the next one.  I tried this, however, using
srand() and irand() (which returns ints, rather than reals), and it
didn't work.  

Thus, though I have no doubts rand() really is linear-congruential, 
it isn't implemented in the most obvious way.  Maybe it uses a Bays-
Durham shuffle (see Numerical Recipes).  You don't have access to 
rand()'s internals, and these could change in future releases.
Since all linear-congruential pseudorandom number generators are in 
some sense created equal, and since writing one takes about ten
line of code, I'd suggest instead rolling your own.  Then, if
it doesn't work, you at least know whom to blame. :-)  Numerical
Recipes makes several suggestions that are easy to implement.
See especially their "quick and dirty" suggestion, which amounts
to:

	iran_new = 1664525 * iran_old + 1013904223
	iran_old = iran_new
	ran_new = iran_new / 2.0**32

Thus, the last number generated becomes the seed for the new one.
For caveats and improvements, see Numerical Recipes.

Final note:  the fact that rand() is based on generation of integers
>from 0 to 2**15-1 suggests that is is a modification of the old, 16-bit
rand() that came with the original 16-bit UNIX.  This also suggests that
you'd do better with something different.

	-P.
************ "There Won't Be Any More."  Charlie Rich, RIP ***********
*Peter S. Shenkin, Box 768 Havemeyer Hall, Chemistry, Columbia Univ.,*
*NY, NY  10027;  [email-masked];  (212)854-5143;  FAX: 678-9039*
********** World music:  What Bluegrass is to a Bulgarian. ***********