lundi 17 mai 2021

random.choice in python, uniform distribution of elements

I have written a function in python which generate dictionaries in this format:

{"A":20,"T":20,"G":30,"C":30} I want each value to be generated in a range of GC 40%-60%

def makeDict(gcRange, min):
     maxC = gcRange[1]-min
     probC = random.choice(np.arange(min, maxC, 2))
     gc = [gcRange[0]-probC, gcRange[1]-probC]
     probG = random.choice(np.arange(gc[0], gc[1], 2))
     at = 100 - probC - probG
     probA = at/2
     probT = at/2
     return({"A":probA, "T":probT, "G":probG, "C":probC})
d=[]
for i in range(1,1000):
    d.append(makeDict([40,60],10))

I understand that the random.choise occurs with replacement and random.sample occurs without replacement. I want it to be with replacement since I am going to generate 1000 dictionaries. However, when I generate the 1000 dictionaries in my loop, I want to have a uniform distribution of those values that were randomly chosen. Is that already occurring in the random.choice function or do I need to set the weights? If yes, how ? in the function or in the for loop? also, why if I do print(len(d)) it will show 999 instead that 1000? In addition, sometimes I do get negative values.why?




Aucun commentaire:

Enregistrer un commentaire