# Generate a prime number in a range with fixed $n$ most significant bits

I'm new to Mathematica so my question might be trivial.

How do I generate a $200-bit$ prime with the condition that the 10 most significant bits in its binary representation are equal to some binary string (for example $1001110011$)?

I know that I can use RandomPrime[{2^199, 2^200 - 1}] to generate primes in the required range, but how do I guarantee the previous condition?

• One can use NextPrime. (1) Set your initial bits. (2) Pick a random integer rand between the lowest and highest values that have those initial bits. (3) Take NextPrime[rand]. (4) If it exceeds the max, rinse and repeat. – Daniel Lichtblau Jan 24 '18 at 17:59

You can find the valid range by using BitShiftLeft on the string and its successor:
RandomPrime[{BitShiftLeft[2^^1001110011,     190],

• @Shadowfirex - don't ask additional questions in a Comment. Start a new Question. If appropriate to add context, link back to the original question. – Bob Hanlon Jan 24 '18 at 19:06