samedi 26 novembre 2022

Recursion function, random choices with probability from list

I need to make a simulator which makes, for the input list, a list (elements are random choices from rdm_lst) of lists. I have:

lst = ["a", "a", "a"]
rdm_lst = ["a", "b", "c"]

def simulator(lst, rdm_lst):
    sim = []
    for i in lst:
        if i == "a":
            sim.append(np.random.choice(rdm_lst, size=1, p=[0.6, 0.2, 0.2]).tolist()[0])
        elif i == "b":
            sim.append(np.random.choice(rdm_lst, size=1, p=[0.2, 0.6, 0.2]).tolist()[0])
        elif i == "c":
            sim.append(np.random.choice(rdm_lst, size=1, p=[0.2, 0.2, 0.6]).tolist()[0])
    return sim

simulator(rdm_lst)

The output is one list, the problem I have is, that I do not know how to repete this function k time, having as a result list of lists, where the input of a simulator() will be the previous list, not the first one. I tried, put:

return simulator(sim)

instead:

return sim

bun an error occurs. Kindly help.




Aucun commentaire:

Enregistrer un commentaire