Skip to content
Advertisement

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.

Advertisement

Answer

Identify how many players must be good by multiplying by 3 / 10 ceil‘d. In the loop, push a random good or bad value to the array. But, also check if you’ve reached the limit of good or bad values to push, in which case push the other value

const players = ["ryan", "austin", "julian", "kelso", "mitch", "adam", "dwight", "edwin", "connor", "george"]
let goodCount = Math.ceil(players.length * 3 / 10);
console.log('Need total of', goodCount, 'good');
const roles = []
for (let i = 0; i < players.length; i++) {
  if (goodCount === 0) {
    // Rest of the array needs to be filled with bad:
    roles.push('Bad'); continue;
  }
  if (goodCount === players.length - roles.length) {
    // Rest of the array needs to be filled with good:
    roles.push('Good'); goodCount--; continue;
  }
  if (Math.random() < 0.3) {
    roles.push('Good'); goodCount--;
  } else {
    roles.push('Bad');
  }
};
console.log(roles);

Remember to use const instead of let when possible, and remember to always declare your variables before using them (such as the i in the for loop), else you’ll implicitly create global variables, and throw errors in strict mode.

Advertisement