
#    Coded by Cody8295                      #
#    http://codyprojects.info               #
#    Random Credit Card Number Generator    #

import random

def two_sum(n): #Returns the difference of the 2 digits of a number above 9
#16... 1+6 = 7....16-9 = 7
	return n-9

def is_valid(cc): #performs the Luhn check on a number
	if cc==0: return False
	total = 0
	nums = []
	count = 1
	for x in str(cc):
		if count%2==0:
			nums.append(int(x))
		else:
			nums.append(int(x)*2)
		count+=1
	for y in nums:
		if y>9:
			total+=two_sum(y)
		else: total+=y
	if total%10==0: return True
	return False

def generate(n, iin): #generates n number of random working credit card numbers
	s = ""
	if iin!="visa" and iin!="mastercard" and iin!="amex" and iin!="discover" and iin!="dinersclub": return "Unrecognized issuer"
	for x in xrange(0, n):
		cc = 0
		while not is_valid(cc):
			if iin=="visa": cc = random.randrange(4000000000000000, 4999999999999999)
			if iin=="mastercard": cc = random.randrange(5100000000000000, 5599999999999999)
			if iin=="amex":
				if random.randrange(0, 2)==0:
					cc = random.randrange(340000000000000, 349999999999999)
				else: cc = random.randrange(370000000000000, 379999999999999)
			if iin=="discover":
				if random.randrange(0, 2)==0:
					cc = random.randrange(6011000000000000, 6011999999999999)
				else: cc = random.randrange(6500000000000000, 6599999999999999)
			if iin=="dinersclub":
				cc = random.randrange(5400000000000000, 5599999999999999)
		s+=str(cc)+"\n"
	return s

def main():
	n = raw_input("\nHow many random working credit card numbers would you like to generate? ")
	iin = raw_input("\nFor what issuer would you like to generate these cards? (visa, mastercard, amex, discover, dinersclub)")
	if not n.isdigit(): n = 10
	if iin=="": iin="visa"
	ccs = generate(int(n), iin)
	print ccs
	output = raw_input("Output to log?")
	if output == "y" or output == "Y":
		f = open("log"+str(random.randrange(11111, 999999999999))+".txt", "w")
		f.write(ccs)
	else: main()

main()