Help! Pseudo Random Function Generator in C
Quote:
>Thanks for the help.... But I was trying to create a pseudo random number
>generator that gave me uniformly distribute numbers between 0-1. Below is
>part of the code that I have used so far. I just can't get it ito stay
>between O and 1. I have tried many variations of this code and have
>gotten all kinds of answers. Thnaks for the help Derek
>/*Begin Code*/
>static unsigned long int next = 1; /*seed*/
>float rand1(void)
> {
> /*Formula to generate pseudorandom number*/
> next = next * 1103515245+12345;
> return (float) (next/65536)%3276;
> }
>void srand1(unsigned int seed)
> {
> next = seed;
> }
I suspect that your receive pseudo-random numbers between 0 and 3275. I don't
know why you have chosen that particular linear polynomial. It looks suspect
to me. Try changing:
return (float) (next/65536)%3276;
to:
return ((next/65536)%3276)/3275.0;
to get a number between [0 .. 1]. You have less than 12 bits of precision
with this RNG, if it does indeed produce a good dispersion.
I would suggest you might like to try your generator on Marsaglia's torture
test. Or better yet, do a web search. I have doubts about the quality of
this implementation.
--
C-FAQ ftp sites: ftp://ftp.eskimo.com ftp://rtfm.mit.edu
Hypertext C-FAQ: http://www.eskimo.com/~scs/C-faq/top.html
C-FAQ Book: ISBN 0-201-84519-9.
Want Software? Algorithms? Pubs? http://www.infoseek.com