jeudi 19 mars 2015

create javascript array(N) of consecutively smaller integers with the sum of 1000

I am working on a text based roll playing game, and i need to generate an array of N values of probabilities, with each consecutive value being smaller (rarer) than the one before, with the total sum equaling 1000. 1000 is important because elsewhere I have probabilities < 1%.


the array is used by this function to generate a weighted random index: with probability being the array in question



function rndProb(probability)
{

chances = new Array();
for(var x=0; x<probability.length ; x++)
{
for(var y=0; y<probability[x]; y++)
{
chances.push(x);
}
}
var px = Math.floor(Math.random() * chances.length+1);
return chances[px];
}


I realize this function is not efficient, but it only needs to run a few times so performance is not important. I need it to be dynamic, so i can change the number of items in the list, but all added equal 1000 ie:



var rankProb = [400,310,160,100,25,5];


something like



function probGen(arrayLength)
{
for(var i=0;i<arrayLength;i++)
{
prob.push(((1000-arrayLength)/arrayLength)+1);
}
return prob;
}


i just can't figure the formula.


Aucun commentaire:

Enregistrer un commentaire