I was wondering if they was an easy way to select a prime number at random. For example, rather than having a rather large array of prime numbers between the set I want, if I could have a random that just found them efficiantly.
I was thinkig about having the random select a number and then check if it is prime but because they isnt really that many prime numbers I dont think this is efficiant.
Well.. you could for example you rand() function... somewhat like this:
1 2 3 4
while(true) {
int r = rand();
if(r==isPrime()) return r;
}
although it will probably be more cpu expensive than your array example, which is space inefficient but has a constant access...
What you could do.. is check all the prime numbers brute-force like, save them in a file for reusage, then load them in an array for quick acces to them. That might be the best solution..
Check this out, I made it a while ago for a friend, I think you might find this useful. It has two operating modes, one that is fast but memory consuming and one that is slow but less memory consuming. Hope it helps! :)