www.delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1994/11/03/09:17:17

From: ANTHONY APPLEYARD <A DOT APPLEYARD AT fs1 DOT mt DOT umist DOT ac DOT uk>
To: djgpp AT sun DOT soe DOT clarkson DOT edu
Date: Thu, 3 Nov 1994 09:39:08 GMT
Subject: Re: Is it random number sequence?

Csizmadia Peter <cspt AT ludens DOT elte DOT hu> said
> int main(int argc, char* argv[]) {int i; srand(atoi(argv[1]));
>    for(i=0; i<30; ++i) printf(" %d", rand()&3); putchar('\n');}
> This simple program produces the following "random number sequence" for
> any seed number:
> 1 2 3 0 1 2 3 0 1 2 3 0 1 2 3 0 1 2 3 0 1 2 3 0 1 2 3 0 1 2
> Is it the normal behavior of the rand() function in GNU C?

alan DOT macdonald AT brunel DOT ac DOT uk (Alan MacDonald) replied on Wed 2 Nov 1994
12:43:46 +0000 (Subject: Re: Is it random number sequence?):-
  > This is normal behaviour for linear congruential generators - the low bits
have short periods (2**bits). You'll get a better sequence if you mask out
some high bits & shift right.

It should be: "if you mask out <all but> some high bits, and shift right".
That is if you want a range from 0 to a power of 2. I more usually write
e.g.:-

#include<math.h>
#define Random() (random()/kerpow) /* random real 0 to 1 */
double kerpow=pow(2.,31);
main(){int i,n;   ------- i=(int)(Random()/n); ------ }
/* i := random integer 0 to n-1 */
  .................................
  PS. Concerning current discussions about NOEMS and RAM FRAME=n etc, I could
buy plenty of RAM's at Chelford Cattle Market any Monday :-) (about 20 miles
south of here, about 3 miles north of Jodrell Bank radiotelescope.)
  .................................
  PPS. Jodrell Bank has a good visitors center, Sat-Sun afternoons in the
winters, all week in the summers.

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019