https://github.com/touhidurrr/randomcryp
A cryptographically secure, feature rich, zero dependency, lightweight and browser friendly random number generator library.
https://github.com/touhidurrr/randomcryp
browser bun cross-platform crypto cryptography deno friendly generator javascript lightweight nodejs number open-source package random typescript web web-crypto zero-dependency
Last synced: about 1 year ago
JSON representation
A cryptographically secure, feature rich, zero dependency, lightweight and browser friendly random number generator library.
- Host: GitHub
- URL: https://github.com/touhidurrr/randomcryp
- Owner: touhidurrr
- License: bsd-3-clause
- Created: 2025-03-23T15:11:22.000Z (about 1 year ago)
- Default Branch: main
- Last Pushed: 2025-04-11T20:50:10.000Z (about 1 year ago)
- Last Synced: 2025-04-12T15:14:04.967Z (about 1 year ago)
- Topics: browser, bun, cross-platform, crypto, cryptography, deno, friendly, generator, javascript, lightweight, nodejs, number, open-source, package, random, typescript, web, web-crypto, zero-dependency
- Language: TypeScript
- Homepage: https://www.npmjs.com/package/randomcryp
- Size: 30.3 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Funding: .github/FUNDING.yml
- License: LICENSE
Awesome Lists containing this project
README
# randomcryp
A cryptographically secure, feature rich, zero dependency, lightweight and browser friendly random number generator library. Only ~5KB in size.
Uses [Crypto.getRandomValues()](https://developer.mozilla.org/en-US/docs/Web/API/Crypto/getRandomValues) to get its randomness.
The spelling is random-creep in case you are wondering.
## Featues
- Lightweight
- Browser friendly
- TypeScript definitions
- Lots of methods and common aliases
- Batteries Included (Zero dependencies)
- Supported in all major JavaScript / TypeScript runtimes (Browser, Node, Deno, Bun etc.)
## Install
```bash
bun add randomcryp # bun
yarn add randomcryp # yarn
pnpm add randomcryp # pnpm
npm install randomcryp # npm
```
## Usage
```ts
import random from "randomcryp";
float(); // 0.190088246732104
```
Or,
```ts
import { rangeInt } from "randomcryp";
rangeInt(1, 10); // 7
```
#### List of Methods
| Method | Description | Aliases |
| -- | -- | -- |
| **`bool(): boolean`** | Generates a random boolean (`true` or `false`). | `bool()` → `true` |
| **`boolean(): boolean`** | Alias for `bool()`. | `boolean()` → `false` |
| **`percentage(p: number): boolean`** | Generates `true` at given percentage of time. | `percentage(20)` → `false` |
| **`probability(p: number): boolean`** | Generates `true` with a given probability `p` and `false` with probability `1-p`. | `probability(0.8)` → `true` |
| **`uSafeInt(): number`** | Generates a random integer between `0` (inclusive) and `Number.MAX_SAFE_INTEGER` (inclusive). | `uSafeInt()` → `4946544243668033` |
| **`float(): number`** | Generates a random number between `0` (inclusive) and `1` (exclusive). | `number()` → `0.190088246732104` |
| **`random(): number`** | Alias for `float()`. | `random()` → `0.9520779718919631` |
| **`hex(length: number = 8, prefix: boolean = false): string`** | Generates a random hex string of the specified length (default 8). Optionally prefixes with '0x'. | `hex(16)` → `d1ef0149c7849844` |
| **`choice(arr: ArrayLike): E`** | Selects a random element from an array. | `choice([1, 2, 3, 4, 5])` → `3` |
| **`pick(arr: ArrayLike): E`** | Alias for `choice()`. | `pick([1, 2, 3, 4, 5])` → `1` |
| **`shuffle(input: string): string`**, **`shuffle(input: Array): Array`** | Returns a new array or string after shuffling the given array or string randomly. | `shuffle([1, 2, 3, 4, 5])` → `[ 1, 3, 2, 5, 4 ]` |
| **`range(min: number, max: number): number`** | Generates a random number (not integer) between given `min` (inclusive) and `max` (exclusive). Throws if `min` > `max`. | `range(1, 5)` → `4.103370176158448` |
| **`rangeInt(min: number, max: number): number`** | Generates a random number (not integer) between given `min` (inclusive) and `max` (exclusive). Throws if `min` > `max`. | `rangeInt(1, 10)` → `8` |
| **`randInt(min: number, max: number): number`** | Alias for `rangeInt()`. | `randInt(1, 100)` → `35` |
| **`safeInt(): number`** | Generates a random integer between `Number.MIN_SAFE_INTEGER` (inclusive) and `Number.MAX_SAFE_INTEGER` (inclusive). `+0` and `-0` both can be generated. `54` bits precision. Not recommended for general usage. | `safeInt()` → `-5802548511349229` |
| **`ifloat(): number`** | Generates a random number between `-1` (inclusive) and `1` (inclusive). Uses `safeInt()` and thus not recommended. | `ifloat()` → `-0.6076475248861822` |
© 2025, Md. Touhidur Rahman.
License: BSD-3-Clause.