I have an array of values:
var my_arr = [/*all kinds of stuff*/]
I have a function that generates a random number, which I use as the index of an element in my_arr...
var RandomFromRange = function (min,max)
{
return Math.floor(Math.random()*(max-min+1)+min);
};
...so I could do stuff like this:
my_arr[RandomFromRange(0,my_arr.length)];
What I want to do is to designate certain elements within my_arr as having "priority", so that RandomFromRange returns 5, say, 25% of the time, returns 4, 14% of the time, and returns any other number...
(100 - 25 - 14)/(my_arr.length - 2)
...% of the time.
As I was doing my research, I came across several posts that describe similar problems, but their answers are not in Javascript, and I, alas, don't have enough math to understand their general principles. Any advice would be appreciated.
This may not be as exact as what you are looking for, but this certainly works. Basically this code returns a random number specified from the min and max like yours, but only after addressing the priority numbers based on the chance given.
First we gotta prioritize your priority numbers in the code. If there is no hit on your priority numbers, that is when we proceed to the normal RNG.
This code applies a single chance on all array of priority numbers. If you want individual chances for each number in the priority list, we gotta modify the structure and change parameters to a single array of objects that holds something like
etc