0

So, a few years ago I have found an exploit in the giftcard system at McDonalds. The basic point is by combining about 15-20 cards, and their codes, I got a point that 3rd and the 7th characters are the same while the others are completely random.

I might have found the same thing in the new Coke festival that is taken in my area (Turkey). I just wanted to ask, how can I develop a code with random module that it will randomly create codes by going over a specific algorithm, for example let's go with the same 3rd and 7th characters again.

By keeping them, randomly generate 8 numbered/lettered codes. Also I think using the while True would be better to generate like infinite amount of them. I will finally add them into a list. However, I need help in the middle part.

import random
import string

def randomStringDigits(stringLength=6):
    lettersAndDigits = string.ascii_letters + string.digits
    return ''.join(random.choice(lettersAndDigits) for i in range(stringLength))

print ("Generating a Random String including letters and digits")
while True:
    print ("First Random String is  ", randomStringDigits(8))
7
  • 1
    Do you have any code so far? Commented Jan 10, 2020 at 21:55
  • I found some on SO, tbh. Commented Jan 10, 2020 at 21:56
  • The one that I have added right now is from SO Commented Jan 10, 2020 at 21:58
  • Please show some examples of expected input/output Commented Jan 10, 2020 at 21:59
  • Ok @AlexW, just in a sec Commented Jan 10, 2020 at 21:59

3 Answers 3

1

You're almost there. I've just tweaked it a little - you could adjust it as is:

import random

numcount = 5
fstring = ""
length_of_code_you_want = 12
position_to_keep_constant_1 = 2 # first position to keep constant is position 3, index 2
position_to_keep_constant_2 = 6 # 2nd position to keep constant is position 7, index 6
constant_1 = "J" # the value of the constant at position 3
constant_2 = "L" # the value of the constant at position 7

for num in range(length_of_code_you_want): #strings are 19 characters long
    if random.randint(0, 1) == 1:
        x = random.randint(1, 8)
        x += 96
        fstring += (chr(x).upper())
    elif not numcount == 0:
        x = random.randint(0, 9)
        fstring += str(x)
        numcount -= 1

list_out = list(fstring)
list_out[position_to_keep_constant_1] = str(constant_1)
list_out[position_to_keep_constant_2] = str(constant_2)
string_out = "".join(list_out)
print(string_out)
Sign up to request clarification or add additional context in comments.

1 Comment

By the way, do not forget the " " on around the letters ! <3
1

Not sure about the legality of that, but the problem is easy enough.

import random
import string

value_of_seven = 7
value_of_three = 3

def randomString(stringLength=10):
    """Generate a random string of fixed length """
    letters = string.ascii_lowercase
    _string = ''.join(random.choice(letters) for i in range(stringLength))
    print ("Random String is ", randomString() )
    return _string

x = 0
string_set = set()
while x <= 10:
    x += 1
    rand_str = randomString()
    if rand_str[-1, 3] is value_of_seven and rand_str[1, 3] is value_of_three:
        string_set.add(rand_str)

But we really need to know, just letters lower case? Upper case?

Also if your trying to generate them with the same things in those places you would still slice at the same point and add the string on the end.

Ok here is working version with your requirements

import random
import string

value_of_seven = '7'
value_of_three = '3'


def _random(stringLength=5):
    """Generate a  string of Ch/digits """
    lett_dig = string.ascii_letters + string.digits
    return ''.join(random.choice(lett_dig) for i in range(stringLength))


if __name__ == '__main__':
    s = _random()
    s = s[:2] + value_of_three + s[2:]
    s = s[:6] + value_of_seven + s[6:]
    print(s)

1 Comment

I did it. Please check the code again that I have published
1

I'm not sure if this is really elegant to do but I'd just keep the random letter in a list, insert the values at their respective positions and then join the list elements in a string.

import random
import string
first = '3' # the first value to place in the 3rd position
second = '7' # the second value to place in the 7th position of the string

def random_string_digits(string_length):
    values = string.ascii_lowercase+string.digits
    output = [random.choice(values) for i in range(string_length)] 
    # the final string length will be equal to string_length+2
    # now that we created the random list of characters, it's time to insert the letter in it
    output.insert(2, first) # insert the first value in the 3rd position
    output.insert(6, second) # insert the second value in the 7th position
    return ''.join(output)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.