how to use proper looping to generate random permutation code in python?
Question:
I need to generate random unique code with permutations, I’m trying to use a fast way by using
import random
FullChar = 'CFKLMNPRTVWXY01234678'
entriess = []
for i in range(5):
unique_code = ''.join(random.sample(FullChar, count))
entry = (unique_code, flg_id, get_id, bcd)
entriess.append(entry)
print(entriess)
output
example
[('6M7Y13F', None, None, None), ('XNMV714', None, None, None), ('4FMRLK7', None, None, None), ('4LYT2C6', None, None, None), ('0R61KNL', None, None, None)]
the printed code is indeed random, but I want to try another method to generate a random unique code with numpy, for the install process I use
pip install numpy
here’s the code
import numpy as np
FullChar = 'CFKLMNPRTVWXY01234678'
entries = []
result =''
test = np.random.permutation(len(FullChar))
for j in test[:7]:
result += FullChar[j]
entry = (result, flg_id, get_id, bcd)
entries.append(entry)
print(entries)
output
example
[('WM6LF7P', None, None, None)]
expected output
example
[('W3PT1N6', None, None, None), ('KVC7864', None, None, None), ('R236FNY', None, None, None), ('XVF3PL2', None, None, None), ('6V2RFKC', None, None, None)]
how to use proper looping to generate permutation random code like import random?
thank you
Answers:
You can pull the indices from FullChar
and then join them together, like so
result = ''.join(FullChar[i] for i in np.random.permutation(len(FullChar)))
import numpy as np
FullChar = 'CFKLMNPRTVWXY01234678'
entries = []
for i in range(5):
result = ''
test = np.random.permutation(len(FullChar))
for j in test[:7]:
result += FullChar[j]
entry = (result, None, None, None)
entries.append(entry)
print(entries)
[('PT46017', None, None, None), ('TRF7P3W', None, None, None), ('4NRXMTC', None, None, None), ('XY8L603', None, None, None), ('1RXN6WL', None, None, None)]
I need to generate random unique code with permutations, I’m trying to use a fast way by using
import random
FullChar = 'CFKLMNPRTVWXY01234678'
entriess = []
for i in range(5):
unique_code = ''.join(random.sample(FullChar, count))
entry = (unique_code, flg_id, get_id, bcd)
entriess.append(entry)
print(entriess)
output
example
[('6M7Y13F', None, None, None), ('XNMV714', None, None, None), ('4FMRLK7', None, None, None), ('4LYT2C6', None, None, None), ('0R61KNL', None, None, None)]
the printed code is indeed random, but I want to try another method to generate a random unique code with numpy, for the install process I use
pip install numpy
here’s the code
import numpy as np
FullChar = 'CFKLMNPRTVWXY01234678'
entries = []
result =''
test = np.random.permutation(len(FullChar))
for j in test[:7]:
result += FullChar[j]
entry = (result, flg_id, get_id, bcd)
entries.append(entry)
print(entries)
output
example
[('WM6LF7P', None, None, None)]
expected output
example
[('W3PT1N6', None, None, None), ('KVC7864', None, None, None), ('R236FNY', None, None, None), ('XVF3PL2', None, None, None), ('6V2RFKC', None, None, None)]
how to use proper looping to generate permutation random code like import random?
thank you
You can pull the indices from FullChar
and then join them together, like so
result = ''.join(FullChar[i] for i in np.random.permutation(len(FullChar)))
import numpy as np
FullChar = 'CFKLMNPRTVWXY01234678'
entries = []
for i in range(5):
result = ''
test = np.random.permutation(len(FullChar))
for j in test[:7]:
result += FullChar[j]
entry = (result, None, None, None)
entries.append(entry)
print(entries)
[('PT46017', None, None, None), ('TRF7P3W', None, None, None), ('4NRXMTC', None, None, None), ('XY8L603', None, None, None), ('1RXN6WL', None, None, None)]