mercredi 10 avril 2019

Better distribution of seed based random between 2 lists

I have a list containing 100 'friends'. Part of it looks like:

Tetris.friends = 
[
    {
        name: "Haroon Bean",
        file_name: "haroon_bean"
    },
    {
        name: "Nathan Cain",
        file_name: "nathan_cain"
    },
    {
        name: "Ashley Burns",
        file_name: "ashley_burns"
    },
    {
        name: "Dennis Clay",
        file_name: "dennis_clay"
    },

I have also a list of 16 'powers'. Part of it looks like:

Tetris.friend_powers = 
[
    {
        name: "speed_2",
        description: "Speed * 2"
    },
    {
        name: "speed_3",
        description: "Speed * 3"
    },
    {
        name: "speed_4",
        description: "Speed * 4"
    },

What I want to do is that each name always returns the same power, even if the friends list order is changed. This way I can ensure each friend will always return the same even if for some reason the list changes, for consistency reasons.

So what I'm doing right now is this:

let n = Tetris.get_random_int
(
    {
        min: 0,
        max: Tetris.friends.length - 1,
        seed: Tetris.random_3
    }
)

let friend = Tetris.friends[n]

...

let name = friend.file_name
let seed = new Math.seedrandom(name)

let n = Tetris.get_random_int
(
    {
        min: 0,
        max: Tetris.friend_powers.length - 1,
        seed: seed
    }
)

let power = Tetris.friend_powers[n]

So basically I first get a friend with global seed, then make a seed based on the name and use it to get a random powers list index.

This works. The problem is this, when I run a test with all the friends list, this is the distribution:

score_2: 7
score_2_d: 4
score_3: 3
score_3_d: 4
score_4: 5
score_4_d: 1
score_5: 6
score_5_d: 7
speed_2: 9
speed_2_d: 5
speed_3: 15
speed_3_d: 3
speed_4: 8
speed_4_d: 9
speed_5: 4
speed_5_d: 10

As you can see the probability is not equal or close among each power. So my question is, how can I distribute the probability more equally, but still having that a friend's name will always return the same ability, regardless of its index, is that even possible? If not I could just use some index based conditionals and try not to change the list's order.




Aucun commentaire:

Enregistrer un commentaire