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;
}
👆 This is a val. Vals are TypeScript snippets of code, written in the browser and run on our servers. Create scheduled functions, email yourself, and persist small pieces of data — all from the browser.