[comp.lang.fortran] Random numbers

cca04@.uk.ac.keele (P.J. Mitchell) (07/23/90)

I am using Meiko's mf77 and it doesn't come with a library of C-like
functions such as chdir, chown, mkdir etc. Therefore I have been
writing a small library of fortran wrappers for these routines to
ease calling them from fortran.

My problem is that C has the two routines srand and rand. The latter
returning an integer in the range 0-32767 (0 -> (2^15)-1). If I am
to maintain compatibility with other unix fortrans then I have to
make rand, drand and irand which are 31, 31 and 63 bits respectively.
How do I generate this accuracy from the 15 bit C rand routine that
the Sequent man pages say is used ?

Any help netters ?
--
--Paul Mitchell (CMA N.Cheshire, DoD#0145)      | Computer Centre,
JANET:  cca04@uk.ac.keele.seq1                  | University of Keele, Keele,
USENET: cca04@seq1.keele.ac.uk@nss.cs.ucl.ac.uk | Staffordshire, ST5 5BG, U.K.
BITNET: cca04%seq1.keele.ac.uk@ukacrl           | 0782 - 621111 ext 3302

ok@goanna.cs.rmit.oz.au (Richard A. O'Keefe) (07/24/90)

In article <483@keele.keele.ac.uk>, cca04@.uk.ac.keele (P.J. Mitchell) writes:
> How do I generate this accuracy from the 15 bit C rand routine that
> the Sequent man pages say is used?

Don't.

Check "man random" to see if you have the BSD random number generator
and "man drand48" to see if you have the AT&T random number generator.
Check books like "Numerical Recipes" for random number generators and
look in back issues of journals like "Applied Statistics".
Look in Knuth Vol 2 ("Numerical Recipes" provides Fortran code for at
least one of the methods he describes, one of the better ones).

-- 
Science is all about asking the right questions.  | ok@goanna.cs.rmit.oz.au
I'm afraid you just asked one of the wrong ones.  | (quote from Playfair)

shenkin@cunixf.cc.columbia.edu (Peter S. Shenkin) (07/24/90)

In article <483@keele.keele.ac.uk> writes:

>My problem is that C has the two routines srand and rand. The latter
>returning an integer in the range 0-32767 (0 -> (2^15)-1). If I am

C has no such thing.  UNIX has such things.  Rand and srand are outdated
relics of the PDP-11 era, and have been replaced on almost all (?) UNIX
systems by random and srandom, which return 32-bit values.  If your UNIX
system supports these, use them and avoid the problem!

In any case, even on 32-bit UNIX machines when called from C, rand returns
random values only in the 16 high-order bits, so when using these functions
even from C it is necessary to do a 16-bit shift to the "right".  Without
this, the sequence of numbers returned alternates strictly between even
and odd (at least it did on a VAX running ULTRIX) -- scarcely pseudorandom
behavior!

	-P.
************************f*u*cn*rd*ths*u*cn*gt*a*gd*jb**************************
Peter S. Shenkin, Department of Chemistry, Barnard College, New York, NY  10027
(212)854-1418  shenkin@cunixc.cc.columbia.edu(Internet)  shenkin@cunixc(Bitnet)
***"In scenic New York... where the third world is only a subway ride away."***

mrm@loglady.Eng.Sun.COM (Marianne Mueller) (07/25/90)

See also Park & Miller, "Random Number Generators: Good Ones Are Hard
to Find", CACM, Vol 31, No 10, Oct 1988, 1192-1201. 

rns@se-sd.SanDiego.NCR.COM (Rick Schubert) (08/01/90)

In <1990Jul24.133823.6608@cunixf.cc.columbia.edu> shenkin@cunixf.cc.columbia.edu (Peter S. Shenkin) writes:

>In article <483@keele.keele.ac.uk> writes:

>>My problem is that C has the two routines srand and rand. The latter
>>returning an integer in the range 0-32767 (0 -> (2^15)-1). If I am

>C has no such thing.  UNIX has such things.  Rand and srand are outdated
>relics of the PDP-11 era, and have been replaced on almost all (?) UNIX
>systems by random and srandom, which return 32-bit values.  If your UNIX
>system supports these, use them and avoid the problem!

I don't know if all the C people have stopped reading this newsgroup, or
if they are asleep, or just don't care, but I've waited about a week and
haven't seen anyone refute this.  What you say about rand() and srand() is
just not true.  They certainly are a part of C.  That is they are a part of
ANSI C and are a part of many (most? all?) pre-ANSI C libraries.  And they
haven't been replaced on any UNIX systems that I know of; in fact, the 3
UNIX systems that I just checked (5.2, 5.3, and 5.4) all have rand()/srand()
but none of them have random() (perhaps random() is BSD).

>In any case, even on 32-bit UNIX machines when called from C, rand returns
>random values only in the 16 high-order bits, so when using these functions
>even from C it is necessary to do a 16-bit shift to the "right".  Without
>this, the sequence of numbers returned alternates strictly between even
>and odd (at least it did on a VAX running ULTRIX) -- scarcely pseudorandom
>behavior!

It's true that most versions of rand() return values in the range 0-32767,
even on 32-bit machines, but they are in the low-order bits.  If VAX/ULTRIX
returns them in the high-order bits, then either it's broken or you've
messed something up (or are confused about left/right vs. high/low).

-- Rick Schubert (rns@se-sd.sandiego.NCR.COM)

rosenkra@convex1.convex.com (William Rosencranz) (08/02/90)

In article <3618@se-sd.SanDiego.NCR.COM> rns@se-sd.SanDiego.NCR.COM (Rick Schubert) writes:
>In <1990Jul24.133823.6608@cunixf.cc.columbia.edu> shenkin@cunixf.cc.columbia.edu (Peter S. Shenkin) writes:
>>In article <483@keele.keele.ac.uk> writes:
>>>[about srand and rand]
>>C has no such thing.  UNIX has such things.  Rand and srand are outdated
>I don't know if all the C people have stopped reading this newsgroup, or
>if they are asleep, or just don't care, but I've waited about a week and

still here, and even awake!

>haven't seen anyone refute this.  What you say about rand() and srand() is
>just not true.  They certainly are a part of C.  That is they are a part of
>ANSI C and are a part of many (most? all?) pre-ANSI C libraries.  And they

according to harbison and steele at least, they are not really part of
the language, per se, but rather are part of a set of libraries, actually
"facilities", deemed standard by the ANSI C. if they were actually
part of the SYNTAX of the language, they would be listed in with the
reserved words (like for, if, else, while, etc). this is really
semantics and splitting hairs, however. C, unlike fortran, does not
provide language support for i/o, for example, but does provide i/o
pretty much "standard" via the libraries. ANSI C just makes things
like printf work the same with compliant libraries. i'd say that if
posix sez something about things like printf and rand, then it might
be stronger to say that they are more "unix" artifacts than C, if you
look at it that way. my green book is in the office so i can't really
answer this...

-bill
rosenkra%c1yankee@convex.com

(my opinion, not my employer's...)

Bill Rosenkranz            |UUCP: {uunet,texsun}!convex!c1yankee!rosenkra
Convex Computer Corp.      |ARPA: rosenkra%c1yankee@convex.com

cca04@keele.ac.uk (P.J. Mitchell) (08/03/90)

From article <139529@sun.Eng.Sun.COM>, by mrm@loglady.Eng.Sun.COM (Marianne Mueller):
> See also Park & Miller, "Random Number Generators: Good Ones Are Hard
> to Find", CACM, Vol 31, No 10, Oct 1988, 1192-1201. 

Well people, thanks for nothing ! All I've had is references like the above
or really useful comments like use the BSD random() function.

Was my posting not clear enough for you ? I said that I am using Meiko C and
fortran and Meiko supply rand()/srand(). Not one person actually answered my
question about how to use *these* functions ! I was just told not to.

I even started toying with bit shifts...

BTW while looking for the source for folded (has anyone got a copy that they's
like to mail me ?) in the comp.source.misc archives (a real laugh from UK
academinc sites) I found, totally by accident, the source for the random()
function and had hacked that so I can use it. All you jocks telling me to
use it and no one bothers to tell me it's PD and I where to pick it up.

Sheesh.
--
--Paul Mitchell (CMA N.Cheshire, DoD#0145)      | Computer Centre,
JANET:  cca04@uk.ac.keele.seq1                  | University of Keele, Keele,
USENET: cca04@seq1.keele.ac.uk@nss.cs.ucl.ac.uk | Staffordshire, ST5 5BG, U.K.
BITNET: cca04%seq1.keele.ac.uk@ukacrl           | 0782 - 621111 ext 3302

simon@castle.ed.ac.uk (Simon Brown) (08/06/90)

In article <528@keele.keele.ac.uk> cca04@keele.ac.uk (P.J. Mitchell) writes:
>From article <139529@sun.Eng.Sun.COM>, by mrm@loglady.Eng.Sun.COM (Marianne Mueller):
>> See also Park & Miller, "Random Number Generators: Good Ones Are Hard
>> to Find", CACM, Vol 31, No 10, Oct 1988, 1192-1201. 
>
>Well people, thanks for nothing ! All I've had is references like the above
>or really useful comments like use the BSD random() function.
>
>Was my posting not clear enough for you ? I said that I am using Meiko C and
>fortran and Meiko supply rand()/srand(). Not one person actually answered my
>question about how to use *these* functions ! I was just told not to.
>

In both the RTE and MEiKOS C environments, the following are provided:

    a) `Traditional' 16-bit V7 randomness:
	extern int rand (void);
	extern void srand (unsigned int seed);

    b) Berkeley-style randomness:
	extern long random (void);
	extern void srandom (unsigned int seed);

    c) Double-precision randomness (in the math library "-lm"):
	extern double drand48 (void);
	extern void srand48 (unsigned long seed);

These can be called from Fortran by using their names with a prepended 
underscore (and using %VAL to ensure that the correct parameter calling 
sequence is used). Link from Fortran using the "-lc" library.

-------------------------------------------------------------------------------
Simon Brown                            simon@meiko.co.uk
Meiko Scientific Ltd.                  simon@uk.ac.ed
(EPCC, Univeristy of Edinburgh)        simon%ed.ac.uk@nsfnet-relay.ac.uk