Re: Way for computing random primes in standard C.



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/

.



Relevant Pages

  • Way for computing random primes in standard C.
    ... Is there a function in the standard C library which returns a prime number which is also pseudo-random? ... RAND_MAX with pre-computed primes and using the output of randto index into it to extract a random prime. ... how is it different to calling srand() with a seed value such as that returned by the timefunction? ...
    (comp.lang.c)
  • Re: random lottery draw funciton
    ... from approximately 18,000 shares. ... I believe rand() uses your operating system's default randsystem call, ... the quality of the pseudo-random numbers depends on your system. ... http://www.andyhsoftware.co.uk/space:: disk and FTP usage analysis tool ...
    (comp.lang.php)
  • Re: Truly random?
    ... Computers aren't very good at generating truly ... If you need truly random numbers for cryptography, a PRNG won't ... If calling rand() or randomalone slows down ... a pseudo-random number. ...
    (comp.lang.c)
  • Re: How to generate random numbers in C
    ... Computers do not generate truly random numbers without hardware support. ... You may want pseudo-random numbers. ... process ID (but NOT in applications where real random numbers are needed, ... the return type of rand() ...
    (comp.lang.c)
  • Re: Why will this not generate a random number!?
    ... >education is mainly based on object oriented design theory where Java ... I cannot for the life of me generate a random number. ... rand() does not generate random numbers. ... How do you know the result you got is NOT pseudo-random? ...
    (comp.lang.c)