aboutsummaryrefslogtreecommitdiff
path: root/src/lib.ts
diff options
context:
space:
mode:
authorTerry Truong <terry06890@gmail.com>2022-03-28 00:06:16 +1100
committerTerry Truong <terry06890@gmail.com>2022-03-28 00:06:16 +1100
commite31a74ff9a71e59eb0cd87c13a38c0934965404c (patch)
tree935bcbf639b1988c7ba1ed3cf584f4cce63f3119 /src/lib.ts
parentbdc3bf69fae2e61aed8b3e41f46d2b0675a88231 (diff)
Make auto-mode more uniformly random and avoid undoing previous actions
Diffstat (limited to 'src/lib.ts')
-rw-r--r--src/lib.ts6
1 files changed, 4 insertions, 2 deletions
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;
}