Skip to content

Commit

Permalink
feat(random-fxhash): add pickUnique()
Browse files Browse the repository at this point in the history
  • Loading branch information
postspectacular committed Jul 8, 2023
1 parent a70c951 commit 89f0209
Showing 1 changed file with 27 additions and 0 deletions.
27 changes: 27 additions & 0 deletions packages/random-fxhash/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import type { IRandom } from "@thi.ng/random";
import { DEFAULT_SEED_128 } from "@thi.ng/random/constants";
import { pickRandom, pickRandomKey } from "@thi.ng/random/pick-random";
import { SFC32 } from "@thi.ng/random/sfc32";
import { pickRandomUnique } from "@thi.ng/random/unique-indices";
import {
weightedRandom,
weightedRandomKey,
Expand Down Expand Up @@ -89,6 +90,9 @@ export const pickKey = <T extends object>(obj: T, rnd: IRandom = RND) =>
* Repeatedly calls given zero-arg `fn` until it returns a different value than
* `orig` (using strict equality `===`).
*
* @remarks
* Also see {@link pickUnique} for selecting multiple unique items.
*
* @param fn
* @param orig
*/
Expand All @@ -100,6 +104,29 @@ export const pickAlt = <T>(fn: Fn0<T>, orig: T) => {
return res;
};

/**
* Same as thi.ng/random's [pickRandomUnique](), but pre-configured to use
* {@link RND} as default PRNG. Picks up to `k` unique values from `src` array
* (each with `maxTrials`) and adds them to given `existing` array (or creates a
* new one by default) and returns it.
*
* @remarks
* Internally uses `Array.includes()` to check for duplicates.
*
* @param k
* @param src
* @param existing
* @param maxTrials
* @param rnd
*/
export const pickUnique = <T>(
k: number,
src: T[],
existing?: T[],
maxTrials = 100,
rnd: IRandom = RND
) => pickRandomUnique(k, src, existing, maxTrials, rnd);

/**
* One-shot wrapper for
* [`weightedRandom()`](https://docs.thi.ng/umbrella/random/functions/weightedRandom.html).
Expand Down

0 comments on commit 89f0209

Please sign in to comment.