Select random variables from list with limit Python

I need to generate unique random lists for 3 different objects, each object can appear once on each lis and each code has to in fixed length of 5.

import random 
#generate random codes
def generator(code, objects):

    for i in range(len(code)):
        x = random.choices(objects)
        code[i] = x[0]

#Check if code is unique
def isSame(code, list):
    if code not in list:
        return False
        return True

#If code is unique, append it to the codeList and increase counter by 1
codeCount = 0
def listAppend(code, list):
    if isSame(code,list) == True:
        print('This code is not unique')
        global codeCount
        codeCount += 1

if __name__ == '__main__':
    codeList = []
    desiredCount = 12
    while codeCount != desiredCount:
        code = [None]*5
        objects = ['a','b','c','d','e','f','g']
        generator(code, objects)

This gives me random unique lists but however I couldn't think of how to make each object appear only once in each unique list.

e.g. ['a', 'g', 'g', 'a', 'e'] ==> 'g' and 'a' has repeated twice where I need them to appear only once. like, ['a','b','c','d','e']

Can anyone think of a good way to do this? Thanks!!

EDIT: each code has to have fixed length of 5. Also I'm using random.choices to use its probability parameter.

Read more here:

Content Attribution

This content was originally published by erikci at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: