Prime Number Generator

Random primes on demand

A prime number is a whole number greater than 1 whose only divisors are 1 and itself: 2, 3, 5, 7, 11, 13 and so on. This tool picks primes at random from a range you choose. Set From and To (2 and 1,000 by default), choose how many primes you want (up to 100) and press Generate. With "No repeats" ticked, every prime in the list is different.

How it works

The generator picks a random whole number in your range using the browser's cryptographic random number generator and tests whether it is prime. If it is not, it throws the number away and tries again. This "rejection sampling" method means every prime in the range is equally likely to be chosen, so the results are not biased towards primes that follow large gaps.

Each candidate is tested by trial division: the number is divided by every integer from 2 up to its square root, and if none divides it exactly, it is prime. Checking only up to the square root is enough, because if n = a × b, one of the two factors must be no larger than √n. For the largest allowed limit of 1,000,000,000 that is at most about 31,600 divisions per candidate, which takes a fraction of a millisecond.

If a range contains no primes at all, such as 24 to 28, the tool tells you so instead of returning nothing. If you ask for more unique primes than the range contains, you get every prime it could find.

Useful facts about primes

What random primes are used for

Maths practice: teachers can generate primes for factorisation and divisibility exercises, or mix them with composite numbers from a random number generator for "prime or not?" quizzes. Programming: primes are used as hash table sizes, as multipliers in hash functions and as test inputs for prime-checking code. Cryptography lessons: small primes are perfect for working through RSA by hand, where you multiply two primes to make a public modulus.

For real cryptographic keys, use a proper cryptography library. Real RSA primes are hundreds of digits long and need probabilistic tests such as Miller-Rabin, far beyond what this educational tool produces.

FAQ

What is the largest prime this tool can generate?

The upper limit is 1,000,000,000, so the largest possible result is 999,999,937, the largest prime below one billion.

Is 1 a prime number?

No. A prime must have exactly two distinct divisors, and 1 has only one. The tool always starts at 2.

Can I get primes in order?

The primes are returned in random order. Paste them into a spreadsheet and sort the column if you need them in order.