Re: Way for computing random primes in standard C.
fieldfallow wrote:[color=blue]
> Is there a function in the standard C library which returns a prime
> number which is also pseudo-random?[/color]
No. :) The ANSI C standard doesn't have that sort of content in it.
[color=blue]
> 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.[/color]
You can use the primeAt() function here:
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:
[color=blue]
> 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?[/color]
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.)
[color=blue]
> Thank you all for the help. I find this group very useful.[/color]
(That is so ironic ...)
--
Paul Hsieh
fieldfallow wrote:[color=blue]
> Is there a function in the standard C library which returns a prime
> number which is also pseudo-random?[/color]
No. :) The ANSI C standard doesn't have that sort of content in it.
[color=blue]
> 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.[/color]
You can use the primeAt() function here:
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:
[color=blue]
> 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?[/color]
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.)
[color=blue]
> Thank you all for the help. I find this group very useful.[/color]
(That is so ironic ...)
--
Paul Hsieh
Comment