jeudi 26 avril 2018

Generating random values from a set of values according to the percentage distribution

So I'm trying to randomly assign a single value from a array of values to a variable but the values should be assigned according to a defined percentage distribution. For example, If we have the values ["cat", "dog", "mouse"] with their percentage distribution as [30,50,20] respectively. Now suppose I need to generate 10 values from the above array randomly. I want the values like: 1. dog 2. dog 3. mouse 4. cat 5. dog 6. cat 7. mouse 8. dog 9. cat 10. dog So, even though the values have been generated randomly, the percentage distribution from the list is intact(cat-3 times, dog-5 times, mouse-2 times). How can I implement this?




Aucun commentaire:

Enregistrer un commentaire