Re: Way for computing random primes in standard C.




"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


.



Relevant Pages

  • Re: Random number generation
    ... Is it possible to implement such an algorithm in C? ... The standard rand() function is one of them, ... and odd numbers respectively 80% and 20% of the time. ... with a bias in favor of even numbers. ...
    (comp.lang.c)
  • Re: The Collatz-Highway
    ... Given only this sequence --- ... giving the next odd number. ... When I say applicable sequences I mean ... where all entries N2transform to 5 by one step. ...
    (sci.math)
  • Re: Collatz Question
    ... How to Construct a Counterexample to the Collatz ... In the negative domain, the conjecture is false for all C ... Does the Collatz sequence obey the laws of nature? ... But that doesn't mean there are actually more even than odd ...
    (sci.math)
  • Re: Collatz Problem: An idea for its solution.
    ... >> 2) If the current value, P, is an odd number then the person will ... >> away from the point and the distance from the person and the point ... >> of P's will have been shown to ultimately decrease. ... >> encountered in the sequence. ...
    (sci.math)
  • Re: Approximating Pi
    ... a sequece which is clearly Cauchy because ... Suppose I write algorithms in a subset of some modern computing ... hypothesize the existence of a sequence of machines M, ...
    (sci.logic)