Random Prime Number Generator

Enter how many primes you want and press the button to get that many distinct random primes below 10,000,000.

Enter how many primes you want and press the button. The page picks random starting points below 10,000,000, walks forward to the next prime from each one, checks it by trial division, and prints the results as a block of numbers. Every prime in a batch is distinct. It is a quick source of primes for math homework, hash-table sizing, test data and classroom examples, and it runs entirely in your browser.

How the primes are chosen

There is one input: the count. For each prime, the tool draws a random integer between 1 and 10,000,000, then increments until it reaches a number with no divisor between 2 and its square root. That number is the prime. Results are collected in a set, so if the same prime turns up twice the duplicate is discarded and another draw is made. Two consequences of this design are worth knowing:

  • The range is fixed at 2 to 9,999,991. 9,999,991 is the largest prime below ten million, and there is no way to ask for primes above that or to restrict the range to, say, two-digit primes. Small primes like 2, 3 and 5 are technically possible but you will almost never see them; most results have six or seven digits.
  • The draw is not perfectly uniform. Because each result is the first prime at or after a random point, a prime that follows a long gap has a slightly larger chance of being chosen than one that follows a short gap. For homework, examples and test data this does not matter; for cryptography it would, and you should not use a browser tool for that anyway.

The count field accepts large values, but each prime costs up to a few thousand divisions to verify, so requests in the thousands take noticeably longer than requests of ten or a hundred. There are 664,579 primes below ten million, which is the hard ceiling on distinct results.

What the output looks like

The primes appear as plain numbers in a single block, in the order they were found, with no labels or commas. Select the block and copy it to move the numbers into a spreadsheet or a script. Sample batches of five look like this:

BatchPrimes returned
15,433,017 · 2,530,831 · 6,624,041 · 810,137 · 1,215,283
28,990,617 · 1,579,297 · 6,135,251 · 9,777,563 · 973,067
38,513,359 · 3,602,041 · 629,081 · 1,441,963 · 7,275,377

Uses

  • Hash tables and modular arithmetic. Bucket counts and modulus values are often chosen to be prime. Drawing a seven-digit prime is faster than looking one up.
  • Teaching primality and factoring. Hand students a batch and have them verify each one by trial division, or mix in a few composites from the Random Odd Number Generator and ask which are prime.
  • Test fixtures. Primes make good test IDs because they never collide with round numbers or with each other's multiples.
  • Puzzles and games. A "guess the prime" round, a lock code that must be prime, a secret number for a number-theory scavenger hunt.
  • Sanity-checking a sieve. If you wrote an Eratosthenes sieve up to ten million, every number this tool produces should appear in your output.

A few facts about primes in this range

  • There are 25 primes below 100, 168 below 1,000, 78,498 below one million and 664,579 below ten million. The density thins out slowly, roughly one number in every 16 near ten million.
  • The largest gap between consecutive primes below ten million is 154, between 4,652,353 and 4,652,507. Gaps this size are why the draw is slightly biased toward primes after a gap.
  • 2 is the only even prime. Every other result the tool gives ends in 1, 3, 7 or 9.
  • Trial division up to the square root, which is what the tool uses, needs at most 3,163 checks for any number in range, so even a batch of a thousand primes finishes quickly.

Related tools in the same family: the Random Even Number Generator for the other half of the integers, the Random Binary Number Generator and Random Hex Generator for other bases, and the Pi Number Generator if what you actually need is digits of pi. Browse all number generators for the rest.

Frequently asked questions

Can I choose the range, for example primes between 1 and 100?

Not on this page. The range is fixed at 2 to 9,999,991, and there is no minimum or maximum input. For small primes you are better off with a printed list: the primes under 100 are 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89 and 97.

Are the numbers actually prime?

Yes. Each candidate is tested by dividing by every integer from 2 up to its square root; only numbers with no divisor in that range are returned. For numbers below ten million this test is exact, not probabilistic.

Can I get the same prime twice in one batch?

No. Results are stored in a set, so a repeat is thrown away and a fresh draw is made. Different batches can of course overlap.

Is this suitable for generating RSA keys?

No. The primes are far too small (seven digits, where RSA needs hundreds), the random source is the browser's Math.random, and the selection is slightly biased toward primes after large gaps. Use a cryptographic library for keys.