https://github.com/shigma/inaba
A collection of random utilities
https://github.com/shigma/inaba
Last synced: 30 days ago
JSON representation
A collection of random utilities
- Host: GitHub
- URL: https://github.com/shigma/inaba
- Owner: shigma
- License: mit
- Created: 2022-08-20T13:08:54.000Z (almost 3 years ago)
- Default Branch: main
- Last Pushed: 2024-10-16T15:32:50.000Z (8 months ago)
- Last Synced: 2025-05-08T20:57:46.308Z (30 days ago)
- Language: TypeScript
- Size: 14.6 KB
- Stars: 7
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# inaba
[](https://codecov.io/gh/shigma/inaba)
[](https://www.npmjs.com/package/inaba)
[](https://www.npmjs.com/package/inaba)
[](https://github.com/shigma/inaba/blob/master/LICENSE)A collection of random utilities.
## Usage
```ts
import Random from 'inaba'Random.bool(0.8) // gets a random boolean
Random.shuffle([5, 1, 4]) // randomly shuffles an array// use custom random function
const random = new Random(() => Math.random())
random.int(0, 10) // gets a random integer
random.pick([1, 2, 3]) // picks a random element from an array
```## API
### new Random(callback)
- **callback:** `() => number` a random function returning [0, 1)
- **returns:** `Random`Creates a random generator with custom random function.
### Random.bool(probability)
- **probability:** `number`
- **returns:** `boolean`Generates a random boolean value with `probability` chance of being `true`.
### Random.real(lower?, upper)
- **lower:** `number` lower bound, defaults to 0
- **upper:** `number` upper bound
- **returns:** `number`Generates a random real number between `lower` (inclusive) and `upper` (exclusive).
### Random.int(lower?, upper)
- **lower:** `number` lower bound, defaults to 0
- **upper:** `number` upper bound
- **returns:** `number`Generates a random integer between `lower` (inclusive) and `upper` (exclusive).
### Random.pick(array, count?)
- **array:** `readonly T[]`
- **count:** `number`
- **returns:** `T | T[]`If `count` is not provided, returns a random element from `array`; otherwise returns an array of `count` random elements from `array`.
### Random.shuffle(array)
- **array:** `T[]`
- **returns:** `T[]`Randomly shuffles an array. It is equivalent to `.pick(array, array.length)`.
### Random.weightedPick(weights)
- **weights:** `Record`
- **returns:** `T`Randomly picks a key from a dict, using corresponding value as weight.