2

What I want is to generate a string in this specific format: l+l+l+d+d+d+d+l+d+l+l+l+l+d+d+d+d+l+d+l+l+l+l+d+d+d+d+l+d+l+l+l+l+d+d+d+d+l+d+l With each l and d being a different string or number. The issue is when I try to generate, the whole thing is the same value/string. But I want it different. Here is an example: What I am getting: lll9999l9llll9999l9llll9999l9llll9999l9l What I need: bfb7491w3anfr4530x2zzbg9891u2rbep8421m9s

def id_gen():
    l = random.choice(string.ascii_lowercase)
    d = random.choice(string.digits)
    id = l+l+l+d+d+d+d+l+d+l+l+l+l+d+d+d+d+l+d+l+l+l+l+d+d+d+d+l+d+l+l+l+l+d+d+d+d+l+d+l
    print(id)

The result:

lll9999l9llll9999l9llll9999l9llll9999l9l

I need this to generate something different :)

2
  • There is a chance it can generate same value, but not always. Commented May 12, 2020 at 3:09
  • you need to use a loop so that random.choice() generates a different element each time. Currently you're only generating one random element which is why it's all the same. Commented May 12, 2020 at 4:32

3 Answers 3

4

This seems to work for me:

def gen_id() :
    pattern = 'lllddddldllllddddldllllddddldllllddddldl'
    digits = [random.choice(string.digits) for i in range(len(pattern))]
    letters = [random.choice(string.ascii_lowercase) for i in range(len(pattern))]
    return ''.join( digits[i] if pattern[i] == 'd' else letters[i] for i in range(len(pattern)) )

testing:

>>> gen_id()
'lnx1066k0hnrd5409d1nhgo1254t6rzyw5165f8v'
>>> gen_id()
'sbc7119f4ythd8845i1afay1900f4wjcv0659b4e'
>>> gen_id()
'yan6228r0nebj5097y7jnwh7065s7osra0391j5f'
>>> 

seems different enough... please, don't forget to import string, random =)

Sign up to request clarification or add additional context in comments.

Comments

0

To not consume the random generator, IMHO this is the best solution:

def gen_id(pattern) :
    l = len(pattern)
    d = pattern.count('d')
    digits = random.choices(string.digits, d)
    letters = random.choices(string.ascii_lowercase, l-d)
    return ''.join( digits.pop() if pattern[i] == 'd' else letters.pop() for i in range(l) )

Comments

-1

You can use this to get a random combination of letters and digits in the desired order:

def letter():
    return random.choice(string.ascii_lowercase)

def digit():
    return random.choice(string.digits)

def id_gen():
    return letter() + digit() + letter() + letter()  # ldll

4 Comments

Thanks for this but it isnt directly what I need. Specifically, I need my id to follow this format: l+l+l+d+d+d+d+l+d+l+l+l+l+d+d+d+d+l+d+l+l+l+l+d+d+d+d+l+d+l+l+l+l+d+d+d+d+l+d+l Not just random strings + digits :)
@xnarf: then what is the issue you have with your code?
What I want is to generate a string in this specific format: l+l+l+d+d+d+d+l+d+l+l+l+l+d+d+d+d+l+d+l+l+l+l+d+d+d+d+l+d+l+l+l+l+d+d+d+d+l+d+l With each l and d being a different string or number. The issue is when I try to generate, the whole thing is the same value/string. But I want it different. Here is an example: What I am getting: lll9999l9llll9999l9llll9999l9llll9999l9l What I need: bfb7491w3anfr4530x2zzbg9891u2rbep8421m9s
@xnarf: I just edited. Kindly update your previous comment to question so it is readable to future readers as well.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.