Good evening. I ask for help in finalizing the algorithm. The essence is this: we have a database of "id", "name", "chance", "counter". The counter cell stores the number of times the item dropped out (a site like a tape measure). The chance stores interest on the loss (for all items in the database in the amount of 100). It is necessary to make the issue with the database, depending on the chance and the number of depositions. (The greater the percentage (chance) of a fallout and the less time it “fell out” - the greater the chance of a fall NOW).
I wrote such an algorithm for the night looking, but it is too "stupid" and it works the same way. Please help!
Algorithm v0.1
var bd = { "id": [7, 9, 10, 14], // Primary keys "ch": [90, 7, 2, 1], // Chance dropped "dp": [0, 0, 0, 0] // Dropped times } function SomeMath() { var temp = [], idshes = []; for( var i = bd.id.length-1; i >= 0; i-- ) { temp[3-i] = bd.ch[i] * bd.dp[3-i]; // Перевертаем массив с процентами. idshes[3-i] = 3-i; } temp.forEach( function ( el, _x, arr ) { arr[ _x ] = [ arr[ _x ], idshes[ _x ] ]; }); temp.sort( function( a, b ) { return b[0] - a[0]; } ); idshes = temp.map( function ( el, _x, arr ) { arr[ _x ] = el[0]; return el[1]; }); // Отсортировали массивы и узнали наименьшее число. Его берем за нужный предмет. bd.dp[ idshes[ idshes.length-1 ] ]++; // Добавляю к этому предмету 1 выпадение. } for( var i = 0; i < 100; i++ ) { SomeMath(); // Вызов ф-и для теста } console.log( bd.ch ); console.log( bd.dp ); Object bd used as the base.
Sort Result Screenshot
Where the top array is the percentage of the dropout, and the bottom number is for each one.
