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.

enter image description here

  • “The greater the percentage (chance) of a fallout and the less time it“ fell out ”- the more chance of a fall NOW” - note that the number of times you have a coin with an eagle does not increase the likelihood of tailing next time. .. And so - take a random number up to 100, and see - if there is less chance of the first - the first one fell out, if there is more chance of the first, but less than the sum of the chances of the first and second - the second, and so on ... - Harry
  • If I, for example, have a Samsung for 10,000 rubles and an epl for 50,000 rubles, the interest will be 70 to 30, for example, again. And I do not need that, by lucky chance, the epl won 3 times in a row. It must be all strictly. - Slavik Okara
  • This is a completely different task. With "the less dropped out - the greater the chance" you are not guaranteed against "happy coincidence". To guarantee - remove the won goods from the auction :) And take into account the main thing - this is to your wording - with poor quality TK the result is usually HZ ... - Harry

1 answer 1

As indicated in the comments, it is not necessary to take into account how many times this ID has already dropped out. In large samples, the drop rate will tend to get the chance to drop this ID. Check for yourself: (Click "Run code")

 var bd = { "id": [7, 9, 10, 14], // Primary keys "ch": [90, 7, 2, 1], // Chance dropped "dp": [0, 0, 0, 0] // Dropped times } for (i=0;i<1000;i++){ rulette=Math.random()*100;//получаем случайный процент sum=0; pos=-1; while (rulette>sum) { //определяем что выпало pos++ sum+=bd.ch[pos] } bd.dp[pos]++ // добавляем к счетчику } document.write( JSON.stringify(bd))// выводим результат 

  • Yes, I understand that. But random is not needed. Such as, for example, when testing your code {"id":[7,9,10,14],"ch":[90,7,2,1],"dp":[899,79,11,11]} should not be. Must fall the same amount of all interest. 900, 70, 20, 10 . This is very important for my purposes. - Slavik Okara
  • one
    How can I put it ... It seems that the author of the question has not yet clearly formulated for himself exactly what he needs ... - Harry