javascript - Select options based on percentage -


i need efficient method select options based on percentages.

for example, assume have 4 colors: black, blue, red , white.

now, i'm going create array filled colors, except, need select them based on following percentages:

black - 80% chance of selection blue - 70% chance of selection red - 30% chance of selection white - 5% chance of selection 

i thought of assigning range between 1 , 100 each color , generating random number between. means % of colors has add 100%, which, might not avoidable really.

code not necessary love algorithms can implemented via javascript accomplish this.

edit: based on patrice levesque's answer, created this test. while haven't verified statistics of it, visually represents i'm after. usual, over-thought problem. other answers more welcome.

you need normalize values; total “percentage” (in instance, 80 + 70 + 30 + 5 → 185) , pick number between 1 , total; in case, 1-80 black, 81-150 blue, 151-180 red , 181-185 white.


Comments

Popular posts from this blog

SPSS keyboard combination alters encoding -

Add new record to the table by click on the button in Microsoft Access -

javascript - jQuery .height() return 0 when visible but non-0 when hidden -