Skip to content
Allin

Test Card Numbers: What the Luhn Algorithm Is Actually For, and What It Cannot Tell You

Published 8/5/2026 · 15 min read · Developer tools

Daniel Okonkwo

Daniel OkonkwoFront-end developer and tech writer at Allin

Web performance · File formats

Checked against 4 sources

View profile
In short

The Luhn formula is a check-digit scheme Hans Peter Luhn filed a patent for in January 1954, granted in August 1960 as US 2,950,048 under the title Computer for Verifying Numbers, and adopted since as the check digit for the card numbering system standardised in ISO/IEC 7812-1. It works from the right: double every second digit, subtract nine from any result above nine, add everything up, and a valid number has a total divisible by ten. On 4242 4242 4242 4242 the eight 4s each become 8 and the eight 2s stay, giving 64 plus 16, or 80 — divisible by ten, so the number passes. That is the whole algorithm and the whole guarantee. Brute-forcing it shows exactly what it buys you: every one of the 144 single-digit substitutions on that number is caught, and swapping two adjacent digits is caught in 88 of the 90 possible cases, the two exceptions being 09 against 90. It also misses the twin errors 22 against 55, 33 against 66 and 44 against 77. It is a typing check, nothing more. It is not a security feature, it cannot be, and a number passing it says nothing about whether an account exists, has funds or would be authorised — only the issuer knows that, and only when your processor asks during authorisation. Running this generator's output confirms the mechanics: 120,000 numbers across six brands, every single one Luhn-valid, correct lengths and correct security-code widths. What that buys you is a way to exercise your form's client-side validation. For actually testing a payment integration, use the numbers your processor documents — Stripe, Adyen and PayPal each publish theirs, and they are designed to trigger specific decline codes. A generated number beginning 4242 is not Stripe's 4242 4242 4242 4242, and it will teach you nothing.

Luhn is a checksum for catching typos, patented in 1960, and that is its entire job. A number that passes it tells you nothing about any account. For testing a payment integration you need your processor's published numbers, not a generated one.

The algorithm, worked through so you can check it by hand

Start at the rightmost digit, which is the check digit, and leave it alone. Move left and double every second digit — the one immediately left of the check digit, then every alternate one after that. Whenever doubling gives a result above nine, subtract nine from it, which is the same as adding the two digits of the doubled value together. Now add every digit in the number, doubled or not. If the total is divisible by ten, the number passes.

Take 4242 4242 4242 4242, which is the most published test number in the world. Sixteen digits, so the doubling positions are the first, third, fifth and so on from the left — all eight of the 4s. Each 4 doubles to 8, which is not above nine, so nothing is subtracted. The eight 2s are untouched. The sum is eight lots of 8, which is 64, plus eight lots of 2, which is 16, giving 80. Eighty divided by ten is eight with nothing left over, so the number is valid. A second example with mixed digits, in case the repetition hides the method: 4539 1488 0343 6467 gives the sequence 8, 5, 6, 9, 2, 4, 7, 8, 0, 3, 8, 3, 3, 4, 3, 7, which also totals 80.

Producing a valid number rather than checking one is the same arithmetic run backwards. Take the digits you want without the last one, compute the sum as above treating the missing position as the check digit, and the check digit is whatever brings the total up to the next multiple of ten. For 424242424242424 the answer is 2. For 37828224631000, an Amex body of fourteen digits, it is 5, which is how 378282246310005 comes to be the published Amex test number. That is all a generator does.

What it catches, brute-forced

The claim usually made for Luhn is that it catches all single-digit errors and almost all transpositions of adjacent digits. Both halves can be checked exhaustively rather than believed. Change one digit of 4242 4242 4242 4242 to each of the nine other values, at each of the sixteen positions, and all 144 resulting numbers fail the check. That part is complete: any single mistyped digit is caught, always.

The transposition half is nearly complete and the exception is precise. Build a valid number ending in each of the ninety ordered pairs of different digits, swap those two digits, and check again: eighty-eight of the ninety swaps break the checksum. The two that survive are 09 becoming 90 and 90 becoming 09. The reason is arithmetic rather than mysterious — doubling 0 gives 0 and doubling 9 gives 18, which reduces to 9, so those two digits contribute the same amount whichever position they sit in, and exchanging them changes nothing. There is a second, less-quoted blind spot in the same family: the twin errors, where a doubled pair is mistyped as another doubled pair. Typing 55 for 22, 66 for 33 or 77 for 44 leaves the total unchanged and passes.

Set against that, notice what the check does not attempt. It has no secret, no key and no state. Anybody can compute a valid number in a second, and the ninety per cent of sixteen-digit strings that fail the check are the only thing it excludes. That is the correct design for what it is for: catching a customer who mistyped one digit of their own card, before you spend a network round trip and a fee finding out. It was never intended to establish that a card exists, and it cannot be made to.

The number, the account and the authorisation are three different things

A card number has structure. The leading digits are the issuer identification number, which identifies the network and the institution and is assigned under ISO/IEC 7812-1; the digits after that are the account identifier the issuer allocated; the last is the check digit. Structure is all it is. Knowing that a number starts with a valid issuer prefix and ends with a correct check digit tells you the string is well formed, in the same way that a correctly formatted postcode tells you nothing about whether a house stands there.

Whether an account stands behind the number, whether it is open, whether it has room for the amount, whether the issuer's own risk rules will allow this merchant on this day — none of that is in the digits. It is answered by an authorisation request: your processor sends the details to the network, the network routes them to the issuer, and the issuer answers with an approval or a decline code. That round trip is the only thing that knows. It is also the only thing that can decline for insufficient funds, or for a card reported lost, or because the security code did not match, which is precisely the range of outcomes your checkout has to handle correctly.

This is why the processors publish test numbers instead of telling you to generate your own. Stripe documents a specific number for each brand and a further set that produce named failures, so you can write a test that asserts your page shows the right message when a card is declined for insufficient funds rather than for an incorrect security code. Adyen publishes its own list and says plainly that those numbers only work with its test platform and do not work on other platforms. PayPal publishes card details for its sandbox. Each list is tied to one processor's test environment, and it exists because the useful thing to test is not whether a string looks like a card but whether your code handles what comes back.

What this generator produces, checked against the published ranges

The mechanics are sound. Twenty thousand numbers per brand across six brands, 120,000 in all, and every one of them passes the Luhn check — not a single failure, re-run after every change to the ranges. Every prefix produced falls inside a range the network actually publishes. The lengths follow the brand: sixteen digits for Mastercard, Discover and JCB, fifteen for American Express, fourteen for the Diners range it uses, and Visa at sixteen digits nine times in ten with thirteen and nineteen at one in twenty each, because Visa issues all three and a form that only accepts sixteen has a bug in it that uniform test data will never show you. The security code widths are right too: three digits everywhere except American Express, where it is four.

Mastercard is the range worth understanding, because of how it is drawn rather than because anything is missing from it. The network was 51 to 55 for decades; since 2017 it has also issued in the 2-series, a span running from 222100 to 272099 that merchants were required to accept from mid-2017. A flat list of prefix strings cannot express that — it is five hundred thousand six-digit prefixes — so each range is held as an inclusive numeric span and a prefix is drawn from inside it. The two ranges are drawn evenly, half each, and that is a test-data decision rather than a picture of the market. Weighted by cards in circulation the 2-series would be a few per cent, and at the tool's default of five cards a developer would never see one, which is exactly the defect worth avoiding. Twenty thousand generated Mastercards split 49.7 to 50.3 between the two. If your validation pattern was written before 2017, or from a generator's output rather than the network's published ranges, it rejects a real 2-series card and you find out when a customer tells you.

A custom prefix no longer overrides the brand, because the brand is no longer taken from the picker at all — it is read off the digits. Type 51 and you get a Mastercard whatever the type selector says. Type a single 9, a first digit ISO/IEC 7812-1 reserves for national assignment and that no network owns, and the card comes back labelled as an unknown BIN on a neutral grey face rather than dressed as a Visa. A short prefix only claims a range when every number it could produce falls inside it: 51 is a Mastercard, a bare 5 is not, because 50 and 56 to 59 are not — so a half-typed BIN reads as unknown, which is the honest answer while the field is still being filled in. The brand read from the digits then drives the rest of the card: the length and the security-code width follow the prefix rather than the dropdown, so a typed Amex BIN gets fifteen digits and a four-digit code. The dropdown of ready-made prefixes never had this problem: choosing one fixes the brand to match.

One more thing about the prefix dropdown, which is the most useful part of the tool and also the easiest to misread. The eleven prefixes on offer — 4242, 4000, 5555, 5200, 3782, 6011, 3566, 3622, 4111, 5100 and 4032 — are the leading digits of numbers the processors publish as test data, not the prefixes of real banks, and the page says so. But a generated number starting 4242 is not Stripe's test card. Stripe's test card is exactly 4242 4242 4242 4242, one specific string; the generator produces a different, random, Luhn-valid number with the same first four digits, and a processor's test environment recognises the documented number rather than the prefix. A chosen prefix also fixes the length to the brand's canonical one, so a 4242 always comes back at sixteen digits rather than one of Visa's rarer thirteen- or nineteen-digit forms. Use the dropdown to make your fixtures look plausible. Use the processor's documentation to make them work.

Each brand the generator offers, and the published ranges it draws from
BrandWhat the tool generatesWhat the network issues
VisaPrefix 4; 16 digits 90% of the time, 13 and 19 at 5% each, 3-digit codePrefix 4; 16 digits is the norm, 13 and 19 also exist
Mastercard51 to 55 and 222100 to 272099, drawn half and half51 to 55 and the 2-series 222100 to 272099, live since 2017
American Express34 and 37, 15 digits, 4-digit code34 and 37, 15 digits, 4-digit code — complete and correct
Discover6011, 65, 644 to 649 and 622126 to 622925, weighted 40/30/20/106011 and 65, plus the 644 to 649 and 622126 to 622925 ranges
JCBAny prefix in 3528 to 3589The whole 3528 to 3589 range
Diners Club36, 300 to 305, 38 and 39, always 14 digits36, 38 to 39 and the 300 to 305 range; 16-digit forms also circulate
Random Credit Card GeneratorGenerate Luhn-valid test card numbers (Visa, Mastercard, Amex…) with expiry and CVV — for software testing only.Try the tool

Frequently asked questions

Does a number passing the Luhn check mean the card is real?
No, and not even slightly. Luhn is a checksum with no key and no secret: anybody can compute a valid number in under a second, and roughly one sixteen-digit string in ten passes by chance. What it excludes is the nine out of ten strings that do not — a filter for typing errors and nothing else. Whether an account exists behind the number, whether it is open, and whether it would authorise a given amount are questions only the issuer can answer, through an authorisation request routed by your payment processor. That is why the check is done in the browser before the request is sent, not instead of it.
Can I use a generated number to test my payment integration?
For the client-side half, yes: a well-formed number with the right length and a valid check digit is exactly what you need to see whether your form accepts it, formats it in groups of four, shows the correct brand icon and asks for three digits rather than four. For everything past that, no. A processor's test environment recognises the specific numbers it publishes, and a random number sharing the first four digits with one of them is not one of them. Stripe, Adyen and PayPal each document a list, and the useful part of those lists is the numbers that fail on purpose — one for insufficient funds, one for a lost card, one for a mismatched security code — because handling a decline correctly is the part of a checkout that actually breaks.
Which transposition errors does Luhn miss?
Exactly one adjacent pair: 09 typed as 90, and 90 typed as 09. Test all ninety ordered pairs of different digits and eighty-eight of the swaps are caught. The exception falls out of the arithmetic: doubling 0 gives 0 and doubling 9 gives 18, which reduces to 9, so the pair contributes 9 either way round. There is a second blind spot people quote less often, in a different family — the twin errors, where a doubled pair is mistyped as another doubled pair. Entering 55 in place of 22, 66 in place of 33, or 77 in place of 44 leaves the checksum unchanged and passes. Both are known limits of a scheme designed in the 1950s to catch a hand-copied digit, and both are irrelevant to security, because Luhn was never a security control.
Why does my validation reject a real Mastercard that starts with 2?
Because the rule was written against the old range. Mastercard was 51 to 55 for decades, and a great many validation patterns still say so. The network added the 2-series in 2017, a range running from 222100 to 272099, and merchants were required to accept it. A pattern that predates that inherits the gap, and so does one derived from a generator that only ever emitted 51 to 55 — which is what this one used to do. It now draws the two ranges in equal measure, so its output covers both; but write the pattern against the network's published ranges rather than against any generator, and treat brand detection from a prefix as maintenance rather than a one-off, because the ranges change.
Is it legal to generate card numbers at all?
Computing a check digit is arithmetic, and arithmetic on its own is not the question. What matters is what the number is used for. Building a test fixture to exercise a form is ordinary software work; attempting to obtain goods or services with a number nobody issued to you is fraud, everywhere, regardless of whether it happens to satisfy a checksum. There is no useful middle ground here, because a generated number cannot buy anything anyway: no account stands behind it and no issuer will authorise it. The practical advice is simply the same as the technical advice — use your processor's published test numbers, keep them in your test suite, and never put a real card number into a development environment, which your contract with that processor and the card industry's own data security standard both have a great deal to say about.

Articles you may find interesting

All guides
GuideTest Data Without Real People: Why Pseudonymised Is Still Personal, and Synthetic Is NotSwapping names for identifiers does not take a database out of the GDPR — Article 4(5) and Recital 26 say so directly. Genuinely synthetic data is outside the Regulation altogether. That single distinction decides how you seed a staging environment.GuideTournament Brackets: Byes, Seeds, and Why the Numbers Must Be Powers of TwoA knockout bracket halves its field every round, so it only closes on a power of two. The bye count, the round count, the seed order and the total number of matches all follow from that one fact — and each is a one-line derivation.How-toHow to Pick a Name at Random — Without Anyone Doubting the ResultA fair draw needs more than a random number: equal probability, no reuse of a biased method, and a result someone else can check. Here is how to run one.GuideSplitting People Into Fair Groups: Random and Fair Are Not the Same Requirement23 people do not divide into fours, and a uniformly random split can hand one group every strong player. Here is the remainder arithmetic, the measured cost of pure randomness, and the stratified fix.ExplainerPoker Hand Probabilities, Derived Rather Than MemorisedEvery five-card poker probability is a counting argument over 2,598,960 hands, and each one fits on a single line. Here are all nine, with the check that proves them: the counts must sum to exactly C(52,5).ExplainerShuffling Is Harder Than It Looks: A Million Runs of the One-Line ShuffleThe shuffle everyone writes — sorting with a random comparator — is biased, and not slightly. A million measured runs show eight of the twenty-four orderings turning up twice as often as the other sixteen.

Related tools

This article is about testing a payment form, and nothing else. A generated number is not a means of payment: no account stands behind it, no issuer will authorise it, and attempting to use one to obtain goods or services is fraud in every jurisdiction this site is published in. Real card numbers are regulated data — the Payment Card Industry Data Security Standard governs how they are stored and handled, and your own contract with your processor will say more. Nothing here is legal, security or compliance advice: use your processor's published test numbers and ask them when in doubt.

Sources

Spotted a mistake in this article?