mercredi 2 septembre 2020

Javascript: Assign percentage of players a random role

Let's say I have these two arrays

let players = ["ryan", "austin", "julian", "kelso", "mitch", "adam", "dwight", "edwin", "connor", "george"]
let roles = []

I would like to populate roles with, let's say 30% of 'Good' and 70% 'Bad' strings in a random order, but always 30% of 'Good' roles.

example: roles: ['Bad','Bad','Bad','Bad','Good','Bad','Bad','Bad','Good','Good']

I am currently running this scenario which randomly creates an array, but without the percent requirements of 'Good' vs 'Bad'.

players: [ ]
roles: []

while (good === false || bad === false) {
    roles = []
    for (i = 0; i < players.length; i++) {
        let randomise = Math.floor(Math.random() * 2)
        if (randomise === 0) {
            roles.push("Good")
            innocent = true
        } else {
            roles.push("Bad")
            traitor = true
        }
    };
}

Can't wrap my head around how I could achieve my goal.




Aucun commentaire:

Enregistrer un commentaire