mardi 20 décembre 2016

generate random weigted string file in python

i'm trying to generate a string from characters of ['A','B','C','D','E'] with length of 3900, and every character should have probability of: {'A':0.1, 'B':0.3, 'C':0.3, 'D':0.1, 'E':0.2 } in this string i wrote the following code:

from random import random
from bisect import bisect

def weighted_choice(choices):
    values, weights = zip(*choices)
    total = 0
    cum_weights = []
    for w in weights:
        total += w
        cum_weights.append(total)
    x = random() * total
    i = bisect(cum_weights, x)
    return values[i]
string_ = ''
for i in range(0,3900):
    string_ = string_ + weighted_choice([("A",10), ("B",30), ("C",30),("D",10),("E",20)])

with open("rand_file","w") as f:
        f.write(string_)

but it doesn't generate the string(file) based on the probabilities. it generates with probabilities like this:

C 0.2500264583 
B 0.2499284457 
E 0.1666428313 
D 0.0833782424 
A 0.0833758065 

probability cause the for loop runs separately every time, without considering previous results.

any help please to solve this problem ?




Aucun commentaire:

Enregistrer un commentaire