Created
May 16, 2017 00:51
-
-
Save sagaya/00149140a1681758fc2242e7af61dbcc to your computer and use it in GitHub Desktop.
Luhn Algorithm for generating random valid credit card numbers with Python
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import random | |
credit_card_number = "" | |
def make_random_number(number_of_element): | |
random_numbers = [] | |
for i in range(number_of_element): | |
random_numbers.append(random.randint(0, 9)) | |
return random_numbers | |
def luhn_algorithm(): | |
random_master_int = make_random_number(13) | |
random_master_int.insert(0,5) | |
random_master_int.insert(1,4) | |
print(random_master_int) | |
sum_even = [] | |
sum_odd = [] | |
for index,element in enumerate(random_master_int): | |
if index % 2 != 0: | |
print(index) | |
r = random_master_int[index] * 2 | |
character_string = list(str(r)) | |
character_int = map(int, character_string) | |
sum_even.append(sum(character_int)) | |
if index % 2 == 0: | |
sum_odd.append(element) | |
total_sum = sum(sum_even)+sum(sum_odd) * 9 | |
random_master_int.append(total_sum % 10) | |
credit_card_number = ''.join(map(str, random_master_int)) | |
print(credit_card_number) | |
luhn_algorithm() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment