Date: Wed, 02 Nov 1994 20:58:01 -0500 (CDT) From: Aaron Ucko Subject: Re: Random Number Generators To: elf AT netcom DOT com Cc: djgpp AT sun DOT soe DOT clarkson DOT edu Organization: Rockhurst College; Kansas City, MO >> int main(int argc, char* argv[]) >> { >> int i; >> srand(atoi(argv[1])); >> for(i=0; i<30; ++i) >> printf(" %d", rand()&3); >> putchar('\n'); >> } >> >> printf(" %d", (rand()>>16)&3); >> > >If the authors of these pseudo-random number code fragments are >interested, my research has shown that neither of these techniques is >very good for selecting uniform deviates (as they are sometimes >called). In fact, it is generally not a good idea to select bits from >a sequence of random numbers because you violate the foundation of the >number generator. They generate random sequences of integers, but not >necessarily random sequences of bits. > >You would do much better to partitioning the random number space: > > deviate = rand (); > if (deviate < RAND_MAX/3) > result = 0; > else if (deviate > (RAND_MAX*2)/3) > result = 2; > else > result = 1; > >Granted, this is an imperfect partition, but it will more closely >generate a sequence with a random distribution of 0's, 1's and 2's. Erm...partititioning into 2^n equal partitions DOES have the effect of examining only the first n bits, you know. But why use rand() anyway when you can use Berkeley's random() or somebody-or-other's rand48(), both of which are included and much more `random.' Just my semi-nybble's worth. --- Aaron Ucko (ucko AT vax1 DOT rockhurst DOT edu; finger for PGP public key) -=- httyp! -=*=-Just because you're paranoid doesn't mean they aren't out to get you.-=*=- Geek code 2.1 [finger hayden AT vax1 DOT mankato DOT msus DOT edu for explanation]: GCS/M/S d(-) H s g+ p? !au a-- w+ v+ C++(+++)>++++ U-(S+)>++++ P+ L>++ 3(-) E-(----) !N>++ K- W(--) M-(--) V(--) po-(--) Y+(++) t(+) !5 j R G tv--(-) b+++ !D(--) B--(---) e>++++(*) u++(@) h!() f(+) r-(--)>+++ n+(-) y?