fix crash when all candidates end up with <=0 odds
This commit is contained in:
parent
ccc2b6e205
commit
20292a7160
1 changed files with 4 additions and 0 deletions
|
@ -21,6 +21,9 @@ export function MakePrediction(election : Election) : Promise<Prediction> {
|
||||||
}
|
}
|
||||||
|
|
||||||
for (const state of Object.keys(election.states)) {
|
for (const state of Object.keys(election.states)) {
|
||||||
|
if (Object.keys(election.states[state].odds).every(p => election.states[state].odds[p] <= 0))
|
||||||
|
for (const candidate of election.candidates)
|
||||||
|
election.states[state].odds[candidate.party] = 1;
|
||||||
var winner = await weightedRand(election.states[state].odds);
|
var winner = await weightedRand(election.states[state].odds);
|
||||||
pred.candidates.find((c : any) => c.party === winner).votes += election.states[state].electoralVotes;
|
pred.candidates.find((c : any) => c.party === winner).votes += election.states[state].electoralVotes;
|
||||||
// @ts-ignore
|
// @ts-ignore
|
||||||
|
@ -62,6 +65,7 @@ export function betterRandom(min : number, max : number) : Promise<number> {
|
||||||
export async function weightedRand(spec : { [key : string] : number }) : Promise<string> {
|
export async function weightedRand(spec : { [key : string] : number }) : Promise<string> {
|
||||||
var i, j, table = [];
|
var i, j, table = [];
|
||||||
for (i in spec) {
|
for (i in spec) {
|
||||||
|
if (spec[i] <= 0) continue;
|
||||||
for (j = 0; j < spec[i] * 10; j++) {
|
for (j = 0; j < spec[i] * 10; j++) {
|
||||||
table.push(i);
|
table.push(i);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue