www.delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1994/11/02/10:21:27

Date: Wed, 2 Nov 1994 12:43:46 +0000
To: djgpp AT sun DOT soe DOT clarkson DOT edu
From: alan DOT macdonald AT brunel DOT ac DOT uk (Alan MacDonald)
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?

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.

---------------------------------------------------------
    Alan DOT MacDonald AT brunel DOT ac DOT uk, +44 895 274000 x2323
Elec Eng, Brunel University, Uxbridge, MIDDX, UB8 3PH, UK
_________________________________________________________



- Raw text -


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