How would I set up a RNG system with two variables where one is more likely to be selected than the other. I know an even 50/50 would be
array = [1,2];
RNG = array[Math.floor(Math.random()*array.length)];
How would I set up a RNG system with two variables where one is more likely to be selected than the other. I know an even 50/50 would be
array = [1,2];
RNG = array[Math.floor(Math.random()*array.length)];
I generally have a mapped array:
var probability = [{value: 1, prob: 0.3}, {value: 3, prob: 0.2}, {value: 0, prob: 0.5}];
Then randomise between 0 and 1 and walk through the array to see which probability area it falls in.
In this example, 0-0.3 is 1, 0.3-0.5 is 3, 0.5-1 is 0.
Or you can just fill the array to match the probability.
var probability = [1, 1, 1, 3, 3, 0, 0, 0, 0, 0];
And just choose a number between 0 and the length of the array.
I get how to get the number between 0 and 1, but how do I go through the array with that number?
Something like:
for(i = 0; i < probability.length; i++)
{
if(prob <= 0.3)
{
value = 1;
}
else if(prob > 0.3 && prob < 0.5
{
value = 3;
}
else
{
etc.
}
}
I feel like this defeats the purpose of the mapped array though…
EDIT: I just realized this wouldn’t work at all the way I thought it would.
This should work, haven’t tested it but you should get the idea:
var rand = Math.random();
var value = probability[0].value;
for (i = probability.length-1; i >= 0; --i) {
if (rand >= probability[i].prob) {
value = probability[i].value;
break;
}
}
This works, just a matter of randomizing the array of images based on the win.