mardi 21 mars 2017

Generate random list of values with certain percentage of occurences of each value in python

I'm looking to generate a list of size N, let's say, where I have m unique elements {v1,v2,...,vm} and I can be certain that there are exactly Np1 occurances of v1, Np2 occurances of v2, ... , Npm occurances of vm, where p1 + p2 + ... + p3 = 1.

As a specific example, I'd like to generate a random 10 element list of 1,2,and 3 where 1 occurs 20% of the time, 2 occurs 30% of the time, and 3 occurs 50% of the time the following lists would be acceptable randomly generated lists:

L1 = [1,3,2,1,3,3,3,2,3,2] L2 = [3,3,3,1,2,1,3,3,2,2]

I'm looking for the most general, pythonic way to achieve this if my input is a list of tuples with (value,probability) pairs and N, the length of the list to be generated. ie for the above example:

T = [(1,0.2),(2,0.3),(3,0.5)] N = 10




Aucun commentaire:

Enregistrer un commentaire