How credit card number generation and the Luhn algorithm work
Payment card numbers issued worldwide adhere to strict structural standards defined by the International Organization for Standardization (ISO/IEC 7812). Whether issued by Visa, Mastercard, American Express, Discover, JCB, or Diners Club, payment card numbers are not random sequences of digits. Instead, they encode the issuing industry, the financial institution, the individual account identifier, and a mathematical checksum known as the Luhn algorithm (Modulo 10).
In software engineering, QA testing, and e-commerce development, engineers need synthetically generated test card numbers to validate checkout flows, card input formatters, and billing engines without handling live financial instruments. If you already have card numbers to inspect, verify their checksums with our credit card validator. Once you verify payment processing flows, you can evaluate interest structures with our APR calculator, examine structured repayment plans with our credit card EMI calculator, compare balance consolidation strategies using our balance transfer calculator, or analyze reward rates with our cash back calculator.
Anatomy of a payment card number (ISO/IEC 7812)
Standard payment cards contain between 13 and 19 numerical digits partitioned into three primary functional segments:
1. Major Industry Identifier (MII) and Issuer Identification Number (IIN/BIN)
The very first digit represents the Major Industry Identifier (MII), which categorizes the issuing institution:
- 1 and 2: Airlines and financial services
- 3: Travel and entertainment (American Express, Diners Club, JCB)
- 4: Banking and financial networks (Visa)
- 5: Banking and financial networks (Mastercard)
- 6: Merchandising and banking (Discover, China UnionPay, Maestro)
- 7: Petroleum and fuel cards
- 8: Telecommunications and healthcare
- 9: National assignment and open testing
The first 6 to 8 digits comprise the Bank Identification Number (BIN) or Issuer Identification Number (IIN). This prefix uniquely identifies the card network, the issuing financial institution, and the card tier (such as debit, business, credit, or platinum).
2. Individual account identifier
The middle sequence (from digit 7 up to the penultimate digit) represents the individual account number assigned by the card issuer. This portion allows issuers to allocate billions of distinct primary account numbers (PANs).
3. The checksum digit (Luhn check digit)
The final digit of the card number is the check digit. It is mathematically calculated using the Luhn algorithm to protect against accidental typing errors, transposition mistakes, and random digit corruption.
The mathematics of the Luhn algorithm (Modulo 10)
Invented by IBM scientist Hans Peter Luhn in 1954, the Luhn formula verifies numerical sequences by applying a checksum formula across the digits.
Step-by-step Luhn validation formula
Given a numerical sequence of length :
- Starting from the rightmost digit (the check digit, position 1 from right) and moving leftwards, leave the first digit unaltered.
- Double the value of every alternating digit (position 2, 4, 6, 8, etc. counting from the right).
- If the product of the doubling operation exceeds 9, subtract 9 from the product (which equals the sum of the product two individual digits, e.g., ).
- Sum all altered and unaltered digits together to produce total checksum :
The card number is valid under the Luhn algorithm if and only if the total sum is an exact multiple of 10:
Calculating the check digit for test data generation
When generating a synthetic test card, we choose an authorized IIN/BIN prefix and fill intermediate account digits randomly up to length . To determine the required check digit , we compute partial sum where the digit immediately preceding the check digit is doubled:
Worked example of Luhn verification
Let us examine the sample 16-digit card number 4532 7500 1234 5673:
| Position (L to R) | Original Digit | Double? (From Right) | Operation | Contribution |
|---|---|---|---|---|
| 1 | 4 | Yes (even pos 16) | 4 x 2 = 8 | 8 |
| 2 | 5 | No (odd pos 15) | unaltered | 5 |
| 3 | 3 | Yes (even pos 14) | 3 x 2 = 6 | 6 |
| 4 | 2 | No (odd pos 13) | unaltered | 2 |
| 5 | 7 | Yes (even pos 12) | 7 x 2 = 14 (14 - 9) | 5 |
| 6 | 5 | No (odd pos 11) | unaltered | 5 |
| 7 | 0 | Yes (even pos 10) | 0 x 2 = 0 | 0 |
| 8 | 0 | No (odd pos 9) | unaltered | 0 |
| 9 | 1 | Yes (even pos 8) | 1 x 2 = 2 | 2 |
| 10 | 2 | No (odd pos 7) | unaltered | 2 |
| 11 | 3 | Yes (even pos 6) | 3 x 2 = 6 | 6 |
| 12 | 4 | No (odd pos 5) | unaltered | 4 |
| 13 | 5 | Yes (even pos 4) | 5 x 2 = 10 (10 - 9) | 1 |
| 14 | 6 | No (odd pos 3) | unaltered | 6 |
| 15 | 7 | Yes (even pos 2) | 7 x 2 = 14 (14 - 9) | 5 |
| 16 | 3 | No (check digit, pos 1) | unaltered | 3 |
Summing all contributions: .
Applying Modulo 10: . Since the remainder is 0, the number is mathematically valid.
Major card networks and technical specifications
| Card Network | IIN / BIN Prefixes | Total Length | CVV Digits | Grouping Format |
|---|---|---|---|---|
| Visa | 4 | 16 (or 13) | 3 digits | 4-4-4-4 |
| Mastercard | 51-55, 2221-2720 | 16 | 3 digits | 4-4-4-4 |
| American Express | 34, 37 | 15 | 4 digits | 4-6-5 |
| Discover | 6011, 622126-622925, 644-649, 65 | 16 | 3 digits | 4-4-4-4 |
| JCB | 3528-3589 | 16 | 3 digits | 4-4-4-4 |
| Diners Club | 300-305, 36, 38 | 14 | 3 digits | 4-6-4 |
| UnionPay | 62 | 16 to 19 | 3 digits | 4-4-4-4 |
Payment gateway sandbox testing best practices
When integrating payment gateways such as Stripe, PayPal, Braintree, Square, or Adyen, developers use synthetic test cards in development sandboxes. Keep these technical guidelines in mind:
- Client-side form validation: Verify that checkout forms correctly apply formatting spaces, validate input lengths, and execute client-side Luhn checks before sending tokenization requests.
- Sandbox gateway responses: Payment gateways map specific test card numbers or CVV values to test scenarios (such as approved charges, 3D Secure challenges, insufficient funds, or expired cards).
- Synthetic cards have no monetary balance: Generated test cards are mathematical constructs. They do not connect to live banking networks or store actual funds. If you need to estimate transaction processing costs for digital wallets, see our Cash App fee calculator.
Frequently asked questions
Can these generated credit card numbers be used to make real purchases?
What errors does the Luhn algorithm detect?
Why does American Express have 15 digits while Visa has 16 digits?
How is a card CVV or CVC code calculated by banks?
What is the difference between a BIN and an IIN?
How can I test credit card interest and installment plans in development?
Resources and references
The formulas and methods in this calculator were checked against these independent sources.