-1

I am wanting to create an Algorithm that creates a 20 digit number code. Similar to how gift card codes are created. I would like to use python for it, since I know python the best out of ALL programming languages. I would like it to create numbers based on certain rules (For ex. Like how certain gift cards have to start with an S and has to have X number of letters in it and numbers) I do not know if I am explaining this clearly, but basically what I want is to create an Algorithm that will create a 20 digit code that has certain rules so people cannot create "false" codes. Thank you! :)

The Code I have currently is

import random

def random_with_N_digits(n):
    range_start = 10**(n-1)
    range_end = (10**n)-1
    return randint(range_start, range_end)

print random_with_N_digits(20)

But I keep getting an error on the print statement?

Layne Trout
  • 105
  • 1
  • 1
  • 8
  • So what have you tried so far? – ronakg Apr 09 '15 at 02:51
  • I haven't tried anything. I understand what an algorithm is, but I can't really find tutorials on how to do it. I can find ones that will create random numbers (password generators basically) and I can find ones that add up the users input digits, but I can't find any that create codes like I have in mind. – Layne Trout Apr 09 '15 at 02:53
  • @LayneTrout You'll want to try something before we help. Do you have particular rules in mind? Are you thinking of something like the Luhn algorithm? – Snakes and Coffee Apr 09 '15 at 02:58
  • The Luhn algorithm is pretty interesting. My question on the Luhn: Does it create random numbers and then creates the check digit to match it? – Layne Trout Apr 09 '15 at 02:59
  • the [very first search result](http://en.wikipedia.org/wiki/Luhn_algorithm) has an implementation of Luhn in python. Modify this and update your question if you run into problems – LinkBerest Apr 09 '15 at 03:17
  • What is the algorithm? – A.J. Uppal Apr 09 '15 at 03:25
  • That is my problem @JGreenwell, I do not understand how to start it? – Layne Trout Apr 09 '15 at 21:31
  • @A.J. Start by [creating a 20 digit random number](http://stackoverflow.com/questions/2673385/how-to-generate-random-number-with-the-specific-length-in-python) (or string if you want to include digits) and assigning that to a variable then test it against a ruleset. here just use if/else or matching function (like [using in](http://stackoverflow.com/questions/5188792/how-to-check-a-string-for-specific-characters). Once you do this update this question with your code and we can help from there. – LinkBerest Apr 09 '15 at 22:32
  • @JGreenwell, this is my code `import random def random_with_N_digits(n): range_start = 10**(n-1) range_end = (10**n)-1 return randint(range_start, range_end) print random_with_N_digits(20) ` It gives me an error with the print statement at the end. If the code doesn't show up, it is off the website you offered me. Scroll down and it says create an arbitrary number. – Layne Trout Apr 09 '15 at 22:49
  • @LayneTrout edit your post and paste it there then modify your question – LinkBerest Apr 09 '15 at 23:10

2 Answers2

1

An algorithm is basically a set of rules to accomplish something, start it as you would start any piece of code. Since your question lacks some data, I will just make an assumption that you want to randomly select a letter or number.

The algorithm will require a list of letters and numbers, and will simply state that: for each number in range from 1-20, you want to 'run' a set of rules.

This set of rules that you want to run is nothing more than a function or some code that can randomly select a letter/number from your data list.

The code above will build you a data list with numbers and letters that you can have the algorithm randomly select from.

import string
from random import randint
data = list(string.ascii_lowercase)
[data.append(n) for n in range(0, 10)]

The code below will range from 0 through 21 (1, 20) and will generate a 20 character string based on a random selection of a number or letter that is inside your data list

x = [str(data[randint(0, len(data)-1)]) for n in range(0, 21)]
y = ''.join(x)
print y
>>> 'km1o07fy9t3j0dktabjsg'
sciweb
  • 51
  • 3
0

One way to do this is to map sequential numbers to your 20-digit codes. That is, you start with the number 1, and you map it to a "code" that might be "1791AQFZ537Y". The number 2 would map to, say, "3894KJAC624H", etc.

You separate the problem into two pieces. First, you want to take a sequential number and make it non-sequential. You do that using a multiplicative inverse. As pointed out in the article, any two sequential values look significantly different.

But still, all you have is a number. Say that using the function above, 1 turned into 973840256539. And you want to turn that into your code that has letters and numbers. Say you want three letters followed by four numbers, etc.

Take the original number, divide by 26 and save it. The remainder will be in the range 0-25. That's your first character (0=A, 1=B, etc.) Do that twice more for the other two letters, saving the value each time you divide it. Then generate the numbers by doing the same thing four times, but using 10 as the divisor.

You'll of course have to fiddle with the encoding to make it fit your particular rules, but what I've described should give you the basic idea.

The resulting code will have the format you want, and it's also reversible. You can take the code, do the reverse of the encoding process to get a number, and then take the multiplicative inverse to get back to the original sequential number.

So if somebody makes up a code, you can quickly reverse engineer it to find the original number that it corresponds to. Say that the you decode a value and it results in the value 137894, but the highest sequential number you've used to generate a code is 50000. You know immediately that the code is not valid.

This technique is not new. It appears that YouTube uses something very similar to create their video keys. For example, the YouTube video key "tCjzSaP0XFE" is a base64-encoded long (64 bit) integer. But the number it decodes to is not the real key. The real key is a sequential number. When the video was created, they took the next sequential key, transformed it using a multiplicative inverse, and then base64 encoded the result to give the published key.

Jim Mischel
  • 131,090
  • 20
  • 188
  • 351