JSFiddle - React, Tailwind, and code Playground

by jcubed111

JavaScript

const stats = [ // [faction, global win rate]
  ['Itars', 173],
  ['Ivits', 142],
  ['Terrans', 113],
  ['Ambas', 111],
  ['Taklons', 98],
  ['Nevlas', 89],
  ['Hadsch Hallas', 77],
  ['Bal T’aks', 74],
  ['Firaks', 58],
  ['Xenos', 56],
  ['Geodens', 53],
  ['Mad Android', 48],
  ['Lantids', 47],
  ['Gleens', 43],
];
let factions = stats.map(([f]) => f);


function weightedRand(choices) {
	let total = choices.map((_, v) => v).reduce((a, b) => a + b);
  let ordered = _.shuffle(choices);
  let t = Math.floor(Math.random() * total);
  
  let next;
  while(t >= 0) {
  	next = ordered.pop();
    t -= next[1];
  }
  return next[0];
}


function runGame() {
	let chosenFactions = ['Itars', ..._.shuffle(factions.slice(1)).slice(0, 4)];
  while(true) {
	  let best = weightedRand(stats);
  	if(chosenFactions.indexOf(best) != -1) {
    	return best;
    }
  }
}


let totals = {};
factions.forEach(f => totals[f] = 0);

for(let i=0; i < 118200; i++) {
	totals[runGame()]++;
}
console.log(totals);