Skip to content

@technobuddha > library > Random > Pick

Function: randomDraw()

ts
function randomDraw<T>(list: readonly T[], random: () => number): 
  | undefined
  | {
  draw: T;
  list: T[];
};

Defined in: random-draw.ts:15

Draw a random item from a list. Returning both the item and the list without the drawn item.

Type Parameters

Type ParameterDefault type
Tunknown

Parameters

ParameterTypeDefault valueDescription
listreadonly T[]undefinedArray of items to pick from
random() => numberMath.randomRandom number generator

Returns

undefined

ts
{
  draw: T;
  list: T[];
}
NameTypeDescriptionDefined in
drawTThe item that was randomly drawn from the listsrc/random-draw.ts:21
listT[]The list with the drawn item removedsrc/random-draw.ts:23

Randomly selected item & the list without the drawn item

Example

typescript
const items = ['a', 'b', 'c'];
randomDraw(items, () => 0.5); // deterministic for example
// { draw: 'b', list: ['a', 'c'] }