Re: Way for computing random primes in standard C.
- From: "Rod Pemberton" <do_not_have@xxxxxxxxxxxxxxxxxxx>
- Date: Fri, 24 Feb 2006 17:14:30 -0500
"fieldfallow" <fieldfallow@xxxxxxxxx> wrote in message
news:dtncv6$h86$1@xxxxxxxxxxxxxxxx
Hello all,
Is there a function in the standard C library which returns a prime
number which is also pseudo-random?
Nope.
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.
Probably not. There are a number of algorithms for finding primes, but,
most are computationally intensive. The larger the prime the longer it will
take to compute it or prove that it is prime.
If you don't want an array, you could generate random even number from 0 to
(RAND_MAX/2). Make them all odd by adding one. Discard and generate
another odd value, if the last decimal digit ends in five, but isn't equal
to five. Then run the odd number into one of the prime number proof
algorithms. It's still computationally intensive.
Just a slight review of primes:
1) zero and one are not prime by mathematicians definitions.
2) two is the only even prime
3) five is the only odd prime whose last decimal digit is five
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?
The algorithm which generates a semi-random or pseudo-random number sequence
has some internal initial values. If you don't call srand(), the sequence
will be semi-random but will be the same sequence every time you run your
program. So, if you were to write a card playing program, you might call
srand() at every shuffle to start a new semi-random sequence and call rand()
to generate the deck of cards. The "randomness" comes from the algorithm in
rand() not from the starting values in generated by srand().
Rod Pemberton
.
- Follow-Ups:
- Re: Way for computing random primes in standard C.
- From: Jordan Abel
- Re: Way for computing random primes in standard C.
- From: Keith Thompson
- Re: Way for computing random primes in standard C.
- References:
- Way for computing random primes in standard C.
- From: fieldfallow
- Way for computing random primes in standard C.
- Prev by Date: Re: I'm not understanding something
- Next by Date: Re: I'm not understanding something
- Previous by thread: Re: Way for computing random primes in standard C.
- Next by thread: Re: Way for computing random primes in standard C.
- Index(es):
Relevant Pages
|