# @lazy-random/generators-math-random2

> 以瀏覽器／Node.js 原生 Math.random 為亂數來源 (Random Number) 的亂數產生器 (Random Number Generator)

Latest version **2.0.1** (published 2026-10-05) · ISC license · 0 weekly downloads

## Install

```sh
npm install @lazy-random/generators-math-random2
pnpm add @lazy-random/generators-math-random2
yarn add @lazy-random/generators-math-random2
bun add @lazy-random/generators-math-random2
```

## Health

**Score 70/100 (B)** — status: active.

Positive: has types; esm support; no vulnerabilities; recently updated; high maintenance score; high quality score.

Warnings: low downloads.

## Facts

| | |
|---|---|
| Version | 2.0.1 |
| Published | 2026-10-05 |
| First published | 2021-12-12 |
| Weekly downloads | 0 |
| License | ISC |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 2 |
| Unpacked size | 35.3 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 2 |
| Author | bluelovers |
| Maintainers | bluelovers |
| Keywords | lazy-random, math-random, math-random2, random-number-generator, rng, entropy-source, function-based, javascript, generator, non-seedable, create-by-yarn-tool, create-by-tsdx |

## Links

- npm: https://www.npmjs.com/package/@lazy-random/generators-math-random2
- Repository: https://github.com/bluelovers/ws-random
- Homepage: https://github.com/bluelovers/ws-random/tree/master/packages/@lazy-random/generators-math-random2#readme
- Issues: https://github.com/bluelovers/ws-random/issues
- npm.io page: https://npm.io/package/@lazy-random/generators-math-random2

## Dependencies (2)

- [@lazy-random/generators-function](https://npm.io/package/@lazy-random/generators-function.md) ^2.0.1
- [@lazy-random/original-math-random](https://npm.io/package/@lazy-random/original-math-random.md) ^2.0.1

## Alternatives

- [random-seedable](https://npm.io/package/random-seedable.md) — 27.9K weekly downloads
- [n2words](https://npm.io/package/n2words.md) — 22.2K weekly downloads
- [@stdlib/math-base-special-factorialln](https://npm.io/package/@stdlib/math-base-special-factorialln.md) — 5.7K weekly downloads
- [@stdlib/math-base-special-abs2](https://npm.io/package/@stdlib/math-base-special-abs2.md) — 1.7K weekly downloads
- [commons-math-interpolation](https://npm.io/package/commons-math-interpolation.md) — 1.4K weekly downloads

## Recent versions

- 2.0.1 (latest) — 2026-10-05
- 1.0.10 — 2023-11-19
- 1.0.8 — 2022-10-29
- 1.0.7 — 2022-01-04
- 1.0.6 — 2022-01-03
- 1.0.5 — 2021-12-21
- 1.0.3 — 2021-12-12

## README

# @lazy-random/generators-math-random2

以瀏覽器／Node.js 原生 `Math.random` 為亂數 (Random Number) 來源的亂數產生器 (Random Number Generator)，隸屬 `@lazy-random` 系列套件。

本套件將原生 `Math.random` 包裝為 `RNGMathRandom2` 類別，繼承自 `@lazy-random/generators-function` 的 `RNGFunction`，讓「以函式 (Function) 為基礎」的亂數來源能與其他演算法共用同一套介面。

## 特色 (Features)

- 直接包裝原生 `Math.random`，無須設定種子 (Seed) 即可使用
- 繼承 `RNGFunction`，可與 `@lazy-random` 系列的其他產生器互換使用
- 同時提供 CommonJS (`dist/index.cjs`) 與 ECMAScript Module (`dist/index.esm.mjs`) 兩種輸出，並附帶型別宣告 (`dist/index.d.ts`)

## 安裝 (Installation)

```bash
yarn add @lazy-random/generators-math-random2
yarn-tool add @lazy-random/generators-math-random2
yt add @lazy-random/generators-math-random2
```

## 使用方式 (Usage)

```ts
import RNGMathRandom2 from '@lazy-random/generators-math-random2'

const rng = new RNGMathRandom2()

console.log(rng.name) // 'math-random2'
```

若要沿用指定的亂數來源函式，可在建構子 (Constructor) 的第一個參數傳入：

```ts
import RNGMathRandom2 from '@lazy-random/generators-math-random2'

const rng = new RNGMathRandom2(myRandomFunction)
```

## API 文件 (API Documentation)

### `class RNGMathRandom2`

繼承自 `RNGFunction<typeof _MathRandom>`，預設以 `@lazy-random/original-math-random` 提供的 `_MathRandom` 作為亂數來源。

#### `constructor(seed?, opts?, ...argv)`

| 參數 (Parameter) | 型別 (Type) | 說明 (Description) |
| --- | --- | --- |
| `seed` | `typeof _MathRandom` | 亂數來源函式 (Random source function)；省略或傳入假值 (Falsy) 時，回退為預設的 `_MathRandom` |
| `opts` | *未限定* | 傳給基類 `RNGFunction` 的選項 (Options) |
| `...argv` | *未限定* | 其餘參數，原樣轉交基類 (Forwarded to the base class) |

#### `get name(): string`

回傳產生器名稱 (Generator Name) `'math-random2'`，用於除錯或辨識目前使用的演算法。

### 預設匯出 (Default Export)

`RNGMathRandom2` 同時為具名匯出 (Named Export) 與預設匯出。

## 開發 (Development)

專案根目錄使用 pnpm + lerna 的 monorepo 結構，本套件常見指令：

```bash
pnpm run build   # 以 tsdx 建置 dist
pnpm run test    # 執行測試
pnpm run lint    # 執行 ESLint
```

## 變更日誌 (Changelog)

請見 [CHANGELOG.md](./CHANGELOG.md)。

## 相關資源 (Related Resources)

- 原始碼倉庫 (Repository)：<https://github.com/bluelovers/ws-random>
- 問題回報 (Issues)：<https://github.com/bluelovers/ws-random/issues>
- 相依套件 (Dependency)：[@lazy-random/generators-function](https://www.npmjs.com/package/@lazy-random/generators-function)、[@lazy-random/original-math-random](https://www.npmjs.com/package/@lazy-random/original-math-random)

---
_Source: https://npm.io/package/@lazy-random/generators-math-random2 · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
