I am trying to write a code to generate random numbers of length l each consisting of a letter and its probability selection where the percentages sum to 100. For example : If I give Random(10,[("X",50),("Y",40),("Z",10)]) should give me "yxxzyyxzzx" i.e 10 random numbers in any fashion(it need not be of 50% of X and 40% of Y and 10% of Z,its just that the sum should be 100(50+40+10)). Similarly Random(5,[("X",50),("Y",40),("Z",10)]) should give me "XXYZZ"
I tried the following code :
from random import choice
def Random(length,weightedchoice):
string=""
for count in range(length):
string+=weightedchoice([("X",50),("Y", 40), ("Z", 10)])
return string