fieldfallow wrote:

> Is there a function in the standard C library which returns a prime

> number which is also pseudo-random?

>

> 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.
I might try something like this

#include <stdlib.h>

int is_prime(int);

/* implementation left out for brevity

*/

int random_prime() {

int candidate = 0;

while (!is_prime(candidate)) {

candidate = rand();

}

return candidate;

}

if the fact that it is slow (probably even *very* slow) and only returns

primes up to RAND_MAX compensates the need for RAND_MAX * sizeof(prime)

bytes of memory used for the array.

--

If you're posting through Google read <http://cfaj.freeshell.org/google>