Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Random prime number

How do I quickly generate a random prime number, that is for sure 1024 bit long?

like image 426
gmile Avatar asked Nov 20 '09 10:11

gmile


People also ask

Why is 11 not a prime number?

The number 11 is divisible only by 1 and the number itself. For a number to be classified as a prime number, it should have exactly two factors.

Is prime number random?

Prime numbers, of course, are not really random at all — they are completely determined. Yet in many respects, they seem to behave like a list of random numbers, governed by just one overarching rule: The approximate density of primes near any number is inversely proportional to how many digits the number has.

Why is 7 not a prime number?

Seven is a prime number because it doesn't have proper factors. In other words, the only factors of 7 are 1 and itself. To be sure of this, let's verify that none of the numbers greater than 1 and less than 7 divides 7. The numbers greater than 1 and less than 7 are 2, 3, 4, 5, and 6.


2 Answers

  1. Generate 1024 random bits. Use a random source that is strong enough for your intended purpose.

  2. Set the highest and lowest bits to 1. This makes sure there are no leading zeros (the prime candidate is big enough) and it is not an even number (definitely not prime).

  3. Test for primality. If it's not a prime, go back to 1.

Alternatively, use a library function that generates primes for you.

like image 154
laalto Avatar answered Oct 29 '22 18:10

laalto


Use a library function, such as OpenSSL. There's no need to write this yourself.

Example: http://ardoino.com/7-maths-openssl-primes-random/

The above link doesn't work so you can use this archive link.

like image 39
Mark Byers Avatar answered Oct 29 '22 16:10

Mark Byers