Help! Pseudo Random Function Generator in C 
Author Message
 Help! Pseudo Random Function Generator in C

Quote:

>I'm a novice at c programming and I need some help. I need help creating a
>pseudo random number generator in C that will produce random numbers
>uniformly distributed over the range 0 to 1.Thanks

The C FAQ has a question:
13.16:  How can I get random integers in a certain range?

Read the answer provided, but use floating point.
If you need a super high quality RNG, then sci.crypt or sci.math.num-analyis
is a much better place to post.  Marsaglia himself posts to both of those
forums.  The sci.crypt FAQ has some stuff on Random Number Generators (RNG's)
--
C-FAQ ftp sites: ftp://ftp.eskimo.com ftp://rtfm.mit.edu
Hypertext C-FAQ: http://www.*-*-*.com/ ~scs/C-faq/top.html
C-FAQ Book: ISBN 0-201-84519-9.
Want Software?  Algorithms?  Pubs? http://www.*-*-*.com/



Fri, 12 May 2000 03:00:00 GMT  
 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



Sat, 13 May 2000 03:00:00 GMT  
 Help! Pseudo Random Function Generator in C

Quote:

> I'm a novice at c programming and I need some help. I need help creating a
> pseudo random number generator in C that will produce random numbers
> uniformly distributed over the range 0 to 1.Thanks


Take a look at the comp.lang.c FAQ. It answers the following questions:
  13.15:  I need a random number generator.
  13.16:  How can I get random integers in a certain range?

You can get the FAQ at http://www.eskimo.com/~scs/C-faq/top.html or
at ftp://rtfm.mit.edu/pub/usenet/comp.lang.c/C-FAQ-list and it gets
posted to this newsgroup and to news.answers regularly (at the
beginning of each month).

Stephan
(initiator of the campaign against grumpiness in c.l.c)



Sun, 14 May 2000 03:00:00 GMT  
 
 [ 5 post ] 

 Relevant Pages 

1. Pseudo random number generator

2. simple pseudo random generator

3. Yet another pseudo-random number generator

4. Pseudo-random number generators

5. C function for random number generator

6. random number generator function

7. need some Random generator functions

8. random number generator random() questionable!!!?

9. HELP: Random number generator??

10. Help with Random Number generators!!!!

11. help in random number generator

12. Help: Need C code for random number generators

 

 
Powered by phpBB® Forum Software