From e31a74ff9a71e59eb0cd87c13a38c0934965404c Mon Sep 17 00:00:00 2001 From: Terry Truong Date: Mon, 28 Mar 2022 00:06:16 +1100 Subject: Make auto-mode more uniformly random and avoid undoing previous actions --- src/lib.ts | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) (limited to 'src/lib.ts') diff --git a/src/lib.ts b/src/lib.ts index e83429d..450eb90 100644 --- a/src/lib.ts +++ b/src/lib.ts @@ -849,8 +849,9 @@ export function updateAscSeq(seq: number[], maxLen: number){ } } } -// Given an array of positive weights, returns a array index chosen with weighted pseudorandomness -export function randWeightedChoice(weights: number[]){ +// Given a non-empty array of non-negative weights, returns an array index chosen with weighted pseudorandomness +// Returns null if array contains all zeros +export function randWeightedChoice(weights: number[]): number | null { let thresholds = Array(weights.length); let sum = 0; for (let i = 0; i < weights.length; i++){ @@ -863,4 +864,5 @@ export function randWeightedChoice(weights: number[]){ return i; } } + return null; } -- cgit v1.2.3