diff --git a/packages/transducers/src/func/weighted-random.ts b/packages/transducers/src/func/weighted-random.ts index 3f8276df50..9f2dfab42a 100644 --- a/packages/transducers/src/func/weighted-random.ts +++ b/packages/transducers/src/func/weighted-random.ts @@ -10,7 +10,7 @@ import { tuples } from "../iter/tuples"; * @param choices * @param weights */ -export function weightedRandom(choices: T[], weights?: number[]) { +export function weightedRandom(choices: ArrayLike & Iterable, weights?: ArrayLike & Iterable) { const n = choices.length; const opts = [...tuples(choices, weights || repeat(1))].sort((a, b) => b[1] - a[1]); let total = 0, i, r, sum; diff --git a/packages/transducers/src/iter/choices.ts b/packages/transducers/src/iter/choices.ts index 1d6c4d1294..9c3b9e316d 100644 --- a/packages/transducers/src/iter/choices.ts +++ b/packages/transducers/src/iter/choices.ts @@ -2,9 +2,9 @@ import { weightedRandom } from "../func/weighted-random"; import { repeatedly } from "./repeatedly"; /** - * Returns an infinite iterator of random choices and their (optional) weights. - * If `weights` are given, it must be the same size as `choices`. If omitted, - * each choice will have same probability. + * Returns an infinite iterator of random choices and their (optional) + * weights. If `weights` is given, it must have at least the same size + * as `choices`. If omitted, each choice will have same probability. * * See: `weightedRandom()` * @@ -16,7 +16,7 @@ import { repeatedly } from "./repeatedly"; * @param choices * @param weights */ -export function choices(choices: T[], weights?: number[]) { +export function choices(choices: ArrayLike & Iterable, weights?: ArrayLike & Iterable) { return repeatedly( weights ? weightedRandom(choices, weights) :