# README

**@buff-beacon-project/rand-utils v1.0.0** • **Docs**

***

## @buff-beacon-project/rand-utils v1.0.0

## Rand Utils

rand-utils is a collection of utilities for working with pre-generated random bits. This is useful for working with public randomness beacon data or any other sources of random bits.

All methods in this library are deterministic and based on the input bits.

### Installation

```bash
npm install @buff-beacon-project/rand-utils
```

### Example

```ts
import { BitReader } from '@buff-beacon-project/rand-utils'
import { randomBytes } from 'crypto'

// create some random bits
const rand = randomBytes(64)
// create a bit reader
const reader = BitReader.from(rand)

// random directions
const directions = ['up', 'down', 'left', 'right']
const randomDirections = reader.unfold((stream) =>
  directions[stream.readBits(2, false)]
)
const list = Array.from(randomDirections)
// => [ 'down', 'right', 'up', 'left', 'down', 'right', 'up', 'left', ... ]

// shuffle an array
const arr = [0, 1, 2, 3, 4, 5, 6, 7]
const shuffled = reader.shuffled(arr)
// => [ 3, 0, 7, 4, 1, 6, 2, 5 ]
```

### Classes

| Class                                         | Description                                    |
| --------------------------------------------- | ---------------------------------------------- |
| [BitReader](/rand-utils/classes/bitreader.md) | Helper class to read bits from an ArrayBuffer. |

### Functions

| Function                                                    | Description                                                                                                      |
| ----------------------------------------------------------- | ---------------------------------------------------------------------------------------------------------------- |
| [boundedRandom](/rand-utils/functions/boundedrandom.md)     | Get an unbiased "random" number within the range `[0, s)` from a bitStream.                                      |
| [getShuffleSeed](/rand-utils/functions/getshuffleseed.md)   | Return an array fully filled with bounded random values appropriate to shuffle a list.                           |
| [shuffle](/rand-utils/functions/shuffle.md)                 | Get a shuffled copy of provided array using the given shuffle seed.                                              |
| [shuffleSelf](/rand-utils/functions/shuffleself.md)         | Shuffle provided array **in place** using the given shuffle seed.                                                |
| [unfoldBitstream](/rand-utils/functions/unfoldbitstream.md) | Utility to yield results from provided function using a bitstream. Stops when bitstream runs out of usable bits. |


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://curby.gitbook.io/rand-utils/readme.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
