https://github.com/blackglory/extra-rand
🌳 Yet another random library
https://github.com/blackglory/extra-rand
browser esm library nodejs npm-package typescript
Last synced: 5 months ago
JSON representation
🌳 Yet another random library
- Host: GitHub
- URL: https://github.com/blackglory/extra-rand
- Owner: BlackGlory
- License: mit
- Created: 2021-03-26T11:36:46.000Z (over 5 years ago)
- Default Branch: master
- Last Pushed: 2025-01-01T15:48:36.000Z (over 1 year ago)
- Last Synced: 2025-08-09T05:57:43.747Z (11 months ago)
- Topics: browser, esm, library, nodejs, npm-package, typescript
- Language: TypeScript
- Homepage: https://www.npmjs.com/package/extra-rand
- Size: 605 KB
- Stars: 1
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
# extra-rand
Yet another random library.
## Install
```sh
npm install --save extra-rand
# or
yarn add extra-rand
```
## API
```ts
interface IRandomNumberGenerator {
next(): number // [0, 1)
}
```
### randomFloat
```ts
function randomFloat(min: number, max: number): number
function randomFloat(generator: IRandomNumberGenerator, min: number, max: number): number
```
The function returns a float in the range `[min, max)`.
### randomInt
```ts
function randomInt(min: number, max: number): number
function randomInt(generator: IRandomNumberGenerator, min: number, max: number): number
```
The function returns an integer in the range `[Math.ceil(min), Math.floor(max))`.
### randomIntInclusive
```ts
function randomIntInclusive(min: number, max: number): number
function randomIntInclusive(
generator: IRandomNumberGenerator
, min: number
, max: number
): number
```
The function returns an integer in the range `[Math.ceil(min), Math.floor(max)]`.
### randomNumber
```ts
enum NumberType {
Float
, Integer
, IntegerInclusive
}
type INumberModel =
| number
| Getter
| {
type: NumberType
min: number
max: number
}
| NonEmptyArray<{
weight: number
model: INumberModel
}>
function randomNumber(model: INumberModel): number
function randomNumber(generator: IRandomNumberGenerator, model: INumberModel): number
```
### randomBool
```ts
function randomBool(probabilityOfTrue: number): boolean
function randomBool(generator: IRandomNumberGenerator, probabilityOfTrue: number): boolean
```
### randomPickItem
```ts
function randomPickItem(items: NonEmptyArray): T
function randomPickItem(
generator: IRandomNumberGenerator
, items: NonEmptyArray
): T
```
### randomPickWeightedItem
```ts
interface IWeightedItem {
weight: number
}
function randomPickWeightedItem(items: NonEmptyArray, weights: NonEmptyArray): T
function randomPickWeightedItem(items: NonEmptyArray): T
function randomPickWeightedItem(
generator: IRandomNumberGenerator
, items: NonEmptyArray
, weights: NonEmptyArray
): T
function randomPickWeightedItem(
generator: IRandomNumberGenerator
, items: NonEmptyArray
): T
```
### randomPickIndex
```ts
function randomPickIndex(items: NonEmptyArray): number
function randomPickIndex(
generator: IRandomNumberGenerator
, items: NonEmptyArray
): number
```
The function returns an integer in the range `[0, items.length]`.
### randomPickWeightedIndex
```ts
function randomPickWeightedIndex(weights: NonEmptyArray): number
function randomPickWeightedIndex(
generator: IRandomNumberGenerator
, weights: NonEmptyArray
): number
```
The function returns an integer in the range `[0, weights.length]`.
### shuffle
```ts
function shuffle(arr: unknown[]): void
function shuffle(generator: IRandomNumberGenerator, arr: unknown[]): void
```