4

I would like to generate a 16 character code

The code has 5 known characters The code has 3 digits The code must be random

What I did :

result1 = "NAA3U" + ''.join((random.choice(string.ascii_uppercase + string.digits) for codenum in range(11)))
2
  • Please tell us what errors/bugs do you see so we can help you. Commented Oct 9, 2021 at 10:53
  • i ve got no error but the code didn't contains 3 digit exactly Commented Oct 9, 2021 at 10:54

3 Answers 3

8

One approach:

import random
import string

# select 2 digits at random
digits = random.choices(string.digits, k=2)

# select 9 uppercase letters at random
letters = random.choices(string.ascii_uppercase, k=9)

# shuffle both letters + digits
sample = random.sample(digits + letters, 11)

result = "NAA3U" + ''.join(sample)
print(result)

Output from a sample run

NAA3U6MUGYRZ3DEX

If the code needs to contain at least 3 digits, but is not limited to this threshold, just change to this line:

# select 11 uppercase letters and digits at random
letters = random.choices(string.ascii_uppercase + string.digits, k=11)

this will pick at random from uppercase letters and digits.

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

7 Comments

In your final bit of code, why did you change k=9 to k=11? That would make the code into an 18-character code rather than a 16-character one, right? You still have the two extra generated digits to make sure it ends up being at least three digits, after all.
@VincentOostelbos Notice that I'm selecting from digits + letters that have length 11, basically that line of code is shuffling the entire string.
Yes, but then there is no guarantee that (at least) two of these will be digits, right? Which, if you want to have at least 3 digits (but possibly more) would be a requirement. So I figured you would still have the string.digits, k=2 line as well (to make sure there are at least 3 digits), but then the total code would grow by 2.
I'm choosing 11 from 11 so there is a guarantee that 2 of them would be from digits
I might be confused. Aren't there 36 possible results in string.ascii_uppercase + string.digits (26 capitalized letters and 10 digits)? Wouldn't it be possible for it to choose 11 capitalized letters and 0 digits, in that case? (Or 10 capitalized letters and 1 digit, which still wouldn't be enough digits for the code, overall.)
|
2

You can add the remaining 2 digits at the end if it is fine for you like this

import random
import string

result1 = "NAA3U" + ''.join((random.choice(string.ascii_uppercase) for codenum in range(8))) + str(random.randint(10,99))

print(result1)

NAA3URYWMGIHG45

8 Comments

i expecting that my code contains only 3 digits inside
Let me know if my edit solves your issue
the code must be totaly random so no you don't resolve it but thanks a lot for your time
@Beluga: A tenth of the time your code will produce a 14 char string. I tested this and I got an example of: NAA3UXGEUFWZX4
Yeah my bad, it should be random.randint(10,99) instead of random.randint(1,99). Missed the extra zero
|
1

You can use random.choices and random.shuffle then use ''.join like below:

>>> import random
>>> import string

>>> def generate_rndm():
...    digit_char = random.choices(string.ascii_uppercase, k=9) + random.choices(string.digits, k=2)
...    random.shuffle(digit_char)
...    return "NAA3U" + ''.join(digit_char)

Output:

>>> generate_rndm()
'NAA3UTVQG8DT8NRM'

>>> generate_rndm()
'NAA3UCYBWCNQ45HR'

>>> generate_rndm()
'NAA3UIJP7W7DLOCQ'

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.