(This may not be possible short of writing a decently sized helper method, but regardless, I'd like to figure it out)
Say I have a list:
{1,3,6}
I'd like to get a random item from that list, BUT, I'd like it to be weighted directly (probability-wise) with the item's value. So if you ran it 100,000 times, the 1
would be selected about 10,000 times, the 3
would be selected about 30,000 times, and the 6
, 60,000 times.
I may just write a helper method by creating ranges like this:
{1,3,6}
Generate random number between 1(inclusive) and 11(exclusive) (sum of list)
if (number == 0)
{
//1
}
else if (number > 0 && number < 4)
{
//3
}
else
{
//6
}
Though that particular example is fairly simple, I'm often working with large lists and they are always different, so it'd be a bit more complex. While I could do it, I'm curious whether there's an easier way.