Skip to content
AgileLogics

Random Prime Generator

Returns a random prime number within a range, with every prime in the range equally likely.

How it works

Choose a minimum and maximum, then click Generate.

How does a random prime generator pick a prime?

It gives each prime between your minimum and maximum the same chance of being chosen. For ranges up to 10,000 numbers wide it lists every prime and picks one; for wider ranges it draws random whole numbers from the range until one is prime. Randomness comes from crypto.getRandomValues, entirely in your browser. Nothing is sent to a server.

Generating a prime

  1. Enter a Minimum and Maximum (the defaults are 2 and 1000).
  2. Click Generate. One prime appears in the output box.
  3. Click again for a different draw, or Copy to grab the number.

The inputs are forgiving. A decimal minimum is rounded up and a decimal maximum rounded down, so 12.7 to 20.5 searches 13 to 20 and the result always sits inside what you typed. A minimum below 2 becomes 2, and an empty field falls back to the default. If the maximum is smaller than the minimum, the tool doesn't guess; it shows "Maximum must be greater than or equal to the minimum."

What you can expect from a few ranges

The number of candidates depends heavily on where the range sits. These counts come from running the tool's own primality check over each range:

Range Primes in range Example output
2 to 100 25 37
2 to 1000 (default) 168 719
24 to 28 0 error message
1,000,000 to 1,001,000 75 1000367

The counts for 100 and 1000 match the prime-counting function π(n) listed in OEIS A000720. Primes thin out as numbers grow: a quarter of the numbers up to 100 are prime, but only 78,498 of the first million (about 7.8%). Roughly, near a number n about one in every ln(n) integers is prime, so a range of a thousand numbers around one million holds only about 72 primes, and the tool found 75.

Why "equally likely" matters

A common shortcut for getting a random prime is to pick a random integer and step upward until you reach a prime. It sounds equivalent but it isn't fair. Below 1000, the biggest gap between primes runs from 887 to 907. With the step-upward method, any start from 888 to 907 lands on 907, so 907 gets twenty times the chances. Its neighbour 883 sits right after 881, so only starts of 882 or 883 reach it, and it gets two. Under that method 907 would turn up ten times as often as 883.

This tool avoids that skew in both of its modes. In a narrow range it builds the full list and chooses an index into it uniformly. In a wide range it draws a uniformly random whole number and, if that isn't prime, throws it away and draws again; it never steps to a neighbour, so 907 and 883 each get exactly one ticket. Both draws come from the site's shared random helper, which takes values from the Web Crypto API (MDN: getRandomValues) and discards out-of-range values instead of using a plain remainder.

How the primality check works

Each candidate n is checked in up to three stages:

  1. Numbers below 2 are rejected, and n is divided by the twelve primes from 2 to 37. A clean division means n is composite (unless n is that prime).
  2. Below 2^32 (about 4.3 billion), odd divisors from 41 upward are tried while divisor × divisor ≤ n. If none divides n evenly, n is prime.
  3. From 2^32 upward, where trial division would need tens of thousands of steps, the tool runs a Miller-Rabin test using those same twelve primes as witnesses.

Stopping trial division at the square root is safe because any factor larger than √n pairs with one smaller than √n, which would already have been found. Checking 997 takes just 12 divisions, by the primes 2 through 37, since 41 × 41 is already past 997.

Miller-Rabin is often described as probabilistic, but with the first twelve primes as witnesses it is proven exact for every number below about 3.2 × 10^23 (OEIS A014233), far beyond the tool's limit. So every answer is a certain prime, never a "probably prime". Speed barely depends on the range:

Range Approximate time (our test)
2 to 1,000 instant
2 to 100,000,000 a millisecond or two
2 to 9,007,199,254,740,991 a few milliseconds
any 10,000-wide window near 9 quadrillion well under 0.1 seconds

Where a random prime is useful

  • Testing your own code. Feed random primes (and the composites next to them) into an isPrime function you wrote while working through the Python lessons and check the answers agree.
  • Hash table and modulus sizes. Picking a prime capacity for an open-addressing table or a multiplicative hash is a classic textbook exercise; ask for a prime between 1000 and 2000 and you have one.
  • Classroom and puzzle work. Factorisation drills, divisibility games and number puzzles all need primes that students can't predict.
  • Base conversion practice. Convert a random prime with the decimal to binary converter; every odd prime ends in a 1 bit.

For a plain random whole number with no primality requirement, use the random integer generator, which can also return up to 100 numbers per click. To check a factor or square root by hand, the scientific calculator is handy.

What this tool doesn't do

It returns one prime per click, with repeats possible between clicks. It doesn't factor numbers or test a single number you type in (though setting minimum and maximum to the same value works as a quick check: a prime comes back, a composite gives the error). It isn't suitable for cryptographic key material. RSA key generation in NIST FIPS 186-5 works with primes far beyond what JavaScript's ordinary numbers can hold exactly.

Last updated

Frequently asked questions

Is 1 a prime number?
No. A prime has exactly two divisors, 1 and itself, and 1 has only one. The generator never returns 1; any minimum below 2 is raised to 2, so the smallest result you can get is 2, the only even prime.
Why do I get "No prime numbers found between X and Y"?
There really are no primes in that range. Runs of consecutive composite numbers are common, for example 24 to 28 or 114 to 126. Widen the range and try again. If the maximum is lower than the minimum you get a different message, "Maximum must be greater than or equal to the minimum.", so you can tell a reversed range from an empty one.
Can it generate several primes at once?
No. Each click returns one prime. Click Generate again for another; each click is an independent draw, so the same prime can come up twice, which is likely in a small range such as 2 to 50 that holds only 15 primes.
How large a range can I use?
The maximum can be anything up to 9,007,199,254,740,991 (2^53 − 1), the largest whole number JavaScript holds exactly; a bigger maximum shows an error. Width doesn't slow it down. Even 2 to that limit returns in a few milliseconds, because wide ranges are sampled rather than scanned.
Can I use these primes for RSA or other cryptography?
No. RSA needs primes hundreds of digits long, generated by procedures such as those in NIST FIPS 186-5. This tool works with ordinary JavaScript numbers and is meant for teaching, testing and puzzles.