1
2
3
4
5
6
7
8
9
10
export function markovChoice(candidates: Record<string, number>) {
const totalWeight = Object.values(candidates).reduce((a, b) => a + b);
let result = null;
let rand = Math.random() * totalWeight;
for (let index = 0; rand >= 0; index++) {
result = Object.keys(candidates)[index];
rand -= Object.values(candidates)[index];
}
return result;
}