The below form generates credit card numbers that have valid Luhn checksums and appropriate pre-fixes.

(Length: 13: 16:)

## Source Code

This was implemented in JavaScript.## About This Generator

First of all, let me make it clear what this generator is **not**: It is not a *legitimate* credit card number generator. It just produces numbers that will pass the initial credit card numerical testing. Numbers generated by this script *could* be valid, since they conform to the general necessary numerical requirements, but statistically it's highly unlikely to generate a valid card number by chance -- the odds are trillions to one (against) of doing so. If you try to use one of the numbers generated here as an actual credit card number, it'll get bounced the moment the card gets validated by the bank that supposedly backs it.

Now, what this generator **is**: In short, it generates number strings with appropriate credit card company prefixes that conform to the established credit card number checksum scheme.

Despite what it might look like, Credit Card numbers aren't totally random strings of numbers. First, the leading digit(s) of the number indicate which credit card company issued the card. Second, all credit card numbers must pass the Luhn algorithm. This requirement restricts the set of valid credit card numbers to a small subset of all possible strings of numbers. It's a simple test that a valid credit card number will pass but that an invalid card will probably not pass. This is how online stores that accept credit cards can reject a credit card number with a typo very efficiently.

All things considered, the Luhn algorithm is very simple, and it's algorithmically trivial to generate a random number and tweak it so as to force it to pass the Luhn test and appear, at a glance, as a valid credit card number.

To use the Luhn algorithm, start at the right side of the credit card number. Moving left, add the first digit to your running sum (which starts at zero), then double the second number and add that to your running sum. However, if the double of the second number yields a double digit number, separate the digits and add them separately (ie, 8 doubles to 16 which is split to 1 and 6 which add to 7, so 7 would be added to the running sum). Continue this pattern of adding the digits in odd-numbered positions from the right and adding the doubled (or doubled, split, and added) the digits in the even-numbered positions from the right. After all digits have been added, take the final sum and divide it by 10. In order to pass the Luhn test, the remainder of division by 10 must be 0 (ie, the final sum must be a multiple of 10).

Statistically if you make a mistake typing your credit card number you will not enter a number that passes the test. If you make a mistake on just one of the numbers you cannot have a valid credit card number because the total must be a multiple of 10, and no one digit can be 10 off from itself without being equivalent to itself modulo 10, thus your mistake must include at least two numbers. But although you likely will not accidentally generate a valid incorrect credit card number, it's not hard to custom tailor a number to pass the Luhn test.

Two facts make it easy to generate a number that passes the Luhn test: The final sum of the number string must be a multiple of 10, and the last digit of the number is never doubled. Thus the final number can be custom tailored based on the rest of the number and force the sum to be a multiple of 10. My script (above) does just this, it generates random numbers right up to the last digit, calculates the sum using the existing digits, then tacks onto the end whatever number is necessary to force the sum to a multiple of 10. (Actually, the first couple numbers are dictated by the specific credit card vendor you want to use, since they all have their own blocks of numbers they're allowed to use.) This process isn't far from how banks generate the credit card numbers themselves, they start the card with their allocated first number(s), then pad the number to a certain length with random digits, then create and append the card's account number, which fills up the card to the very last digit, which is then chosen specifically to make the card pass the Luhn test.

At the top of this page is my credit card number generator, written in JavaScript. It will generate card numbers that pass the Luhn test and fall into the numerical category of the vendor you choose, with the appropriate card number length. But, again, these are not valid credit card numbers, they simply pass typo testing. You can validate that these cards pass the Luhn test by putting them through the test form here. You can read more about credit card numbers here.