lundi 7 octobre 2019

Math.Random() selects some more when selecting items an array

I want to choose one of the items I have according to the possibilities( according square p).

For this;

for (let sec_index = 0; sec_index < 1000;) { //i want select 1000 pcs
                var random_item_num = Math.floor(Math.random() * posts_array.length); //select random item and return index number
                var random_item = posts_array[random_item_num]; //return random item key.
                var random_item_real_prob = prob_map[random_item]; //return random item's p ,  0.56489321 etc... '.
                var random_item_prob = Math.pow(random_item_real_prob, 2) * s_loc; //s_loc is a coefficient
                var random_sayi = Math.random(); //new random number
                if (random_sayi<random_item_prob) {
                    selection_array[sec_index] = random_item; // return [[1: id][2:id]......]
                    selection_count[random_item_prob1]++; //return [[p : 1] [other p : 10] ......]
                    sec_index++;
                }
            }

These codes is still work.

BUT ;

p , selection count in 1000

As you will see here, it makes too much choice for some possibilities. This repeats every time. Although their positions in Array are constantly changing, the result is the same.

Is that a mathematical problem?

Thank You!




Aucun commentaire:

Enregistrer un commentaire