Nearsighted Paramecium Multiverse

# npm

Learn about our RFC process, Open RFC meetings & more.Join in the discussion! »

## atlas-random

1.1.0 • Public • Published

# atlas-random

A set of math utilities for working with random numbers/arrays.

## install

``````npm install --save atlas-random
``````

## why

I need functions which reliably generate uniformly random samples and shuffles of an array.

## examples

### insert element into random position in array

The element will be inserted, uniformly, into a random index in constant time. If you build up an array from scratch via sequential `insert`s, its elements will be uniformly distributed along the indices. This method does not necessarily preserve the existing order of other elements in the array.

### get a random sample from an array

This is done quickly in linear time. You will not get duplicates, and the resulting elements in the set will be uniformly chosen at random. Elements will not be favored based on their position. If you pass in a size which is greater or equal to the size of the array, you will get back a new array of all of the elements in their original order. For every other case, the order of elements in the returned array is not necessarily uniform. Use the `shuffle` method if you want to randomly sort an array in-place.

### shuffling an array

A uniform shuffle is when you sort the elements randomly and uniformly. You can do this two ways with this library.

#### using the `shuffle` method (in-place)

The `shuffle` method uses the Fisher-Yates shuffle algorithm to shuffle your array in-place, meaning your array will be mutated without creating a new array. It is very fast. If you don't want to damage the original array, you could create a copy of your array (e.g. `const copy = [...arr]`), then use the `shuffle` method on the copy.

#### using the `insert` method

Unlike `shuffle`, the insert method can uniformly shuffle an array by inserting all of its elements into a new array.

## Keywords

### Install

`npm i atlas-random`

1

1.1.0

Apache-2.0

12.1 kB

6

### Repository

github.com/atlassubbed/atlas-random