Re: Way for computing random primes in standard C.
- From: websnarf@xxxxxxxxx
- Date: 27 Feb 2006 18:12:40 -0800
fieldfallow wrote:
Is there a function in the standard C library which returns a prime
number which is also pseudo-random?
No. :) The ANSI C standard doesn't have that sort of content in it.
Assuming there isn't, as it appears from the docs that I have, is there
a better way than to fill an array of range 0... RAND_MAX with
pre-computed primes and using the output of rand() to index into it to
extract a random prime.
You can use the primeAt() function here:
http://www.pobox.com/~qed/primeat.zip
indexed by a pseudo random number. This will give exactly even
probabilities for all the primes that are at most 32 bits.
Now, of course you may need a range larger than 0 ... RAND_MAX. You
can build that from code found here:
http://www.pobox.com/~qed/random.html
Also what is meant by reinitialising the rand() function with srand(1)?
Does it mean that further calls to rand() will return numbers with a new
starting point? If so, how is it different to calling srand() with a
seed value such as that returned by the time() function?
rand() outputs numbers in a deterministic sequence indexed by the seed
passed to srand(). Calling srand(time(NULL)) makes the sequence change
over time (usually different for every second of time.)
Thank you all for the help. I find this group very useful.
(That is so ironic ...)
--
Paul Hsieh
http://www.pobox.com/~qed/
http://bstring.sf.net/
.
- References:
- Way for computing random primes in standard C.
- From: fieldfallow
- Way for computing random primes in standard C.
- Prev by Date: Re: trying to make my "fillvalues" function work...
- Next by Date: Re: trying to make my "fillvalues" function work...
- Previous by thread: Re: Way for computing random primes in standard C.
- Next by thread: Type safety
- Index(es):
Relevant Pages
|