### Efficient Way To Do "Weighted" Random Generation?

Posted:

**Mon Jan 26, 2015 11:26 am**I was up late last night doing some research on how to do weighted random number/element generation and found this article in the forums...

viewtopic.php?p=21319

...and this external source on random generation:

http://chancejs.com/#weighted

These sound like what I'm looking to do but is there a more efficient way to generate weighted numbers/elements than having to list X,X,X,X,X a number of times in an array?

If I have four objects: apples, oranges, pears, bananas and I want apples to come up 50% of the time, oranges to come up 25% of the time, pears 5%, and bananas 20%, do I really have to list them 50 times, 25 times, 5 times, and 20 times or is there another math function/variable storage process to handle that more efficiently?

Does that make sense?

viewtopic.php?p=21319

...and this external source on random generation:

http://chancejs.com/#weighted

These sound like what I'm looking to do but is there a more efficient way to generate weighted numbers/elements than having to list X,X,X,X,X a number of times in an array?

If I have four objects: apples, oranges, pears, bananas and I want apples to come up 50% of the time, oranges to come up 25% of the time, pears 5%, and bananas 20%, do I really have to list them 50 times, 25 times, 5 times, and 20 times or is there another math function/variable storage process to handle that more efficiently?

Does that make sense?