What does this mean for me? You will always be able to play your favorite games on Kongregate. However, certain site features may suddenly stop working and leave you with a severely degraded experience.
What should I do? We strongly urge all our users to upgrade to modern browsers for a better experience and improved security.
We suggest you install the latest version of one of these browsers:
Kongregate is a community-driven browser games portal with an open platform for all web games.
Get your games in front of thousands of users while monetizing through ads and virtual goods.
Learn more »
I have 6 varibiles
They can be set to any number really but for the sake of this example Ill give them some random ones.
x1:int = 4
x2:int = 2
x3:int = 3
x4:int = 6
x5:int = 5
x6:int = 3
These numbers represent chances of colours getting picked.
Red corresponds with x1
Orange corresponds with x2
Yellow corresponds with x3
Green corresponds with x4
Blue corresponds with x5
Purple corresponds with x6
So out of all the colours red for instance has a 4 in 23 chance of getting picked. (Because red is represented by x1 (4) and x1+x2+x3+x4+x5+x6 = 23, aka 4 in 23)
I’m going to create 6 new variables to represent these chances as a visual (this may not be necessary in your solution)
y1:int = 4/23
y2:int = 2/23
y3:int = 3/23
y4:int = 6/23
y5:int = 5/23
y6:int = 3/23
What I need is a way to randomly pick and output red,orange,yellow,green,blue, or purple based on their chances of getting picked (the x values.)
iterate over the array,
sum the elements up,
if the sum is bigger than the random Number, pick the current element.
Yes, normalizing to make the sum 1 is a good idea, because that’s what probabilities are like.
> *Originally posted by **[evan999333](/forums/4/topics/335572?page=1#posts-7024981):***
> Also I don’t really understand what NineFiveThree is saying with
> “if the sum is bigger than the random Number, pick the current element.”He probably means that you'll want to make a loop that checks for something like this:  \> randomNumber, + \> randomNumber, ++ \> randomNumber, ...etc. Make a variable that keeps track of the sum of the previous array elements as you iterate through the loop, and check that sum against the random number, thus: if (sum \> randomNumber) break out of loop and return the current array index, because it corresponds with the selected color.