Random prime number

后端 未结 6 1660
花落未央
花落未央 2020-12-30 03:58

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

6条回答
  •  隐瞒了意图╮
    2020-12-30 04:36

    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.

提交回复
热议问题