# @lazy-random/util-probabilities

> 將總和 (Sum) 拆分成指定份數的機率 (Probability) 工具函式 (Utility Function)

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

## Install

```sh
npm install @lazy-random/util-probabilities
pnpm add @lazy-random/util-probabilities
yarn add @lazy-random/util-probabilities
bun add @lazy-random/util-probabilities
```

## 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 | 0 |
| Unpacked size | 62.3 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 2 |
| Author | bluelovers |
| Maintainers | bluelovers |
| Keywords | lazy-random, util-probabilities, probability, sum, random-sum, split, distribution, random, math, stats, array, integer, float, create-by-yarn-tool, create-by-tsdx |

## Links

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

## 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-03
- 1.0.5 — 2021-12-12

## README

# @lazy-random/util-probabilities

將總和 (Sum) 拆分成指定份數的簡單機率 (Probability) 工具函式 (Utility Function)，提供整數與浮點數兩種版本，保證拆分後的陣列 (Array) 總和與輸入一致。

## 特色 (Features)

- `get_prob()`：以整數 (Integer) 拆分總和，最後一筆以差值補齊，確保總和完全相符。
- `get_prob_float()`：以浮點數 (Float) 拆分總和，同樣以差值補齊尾數。
- 純函數 (Pure Function)，無任何相依套件 (Dependency)。

## 安裝 (Installation)

```bash
yarn add @lazy-random/util-probabilities
yarn-tool add @lazy-random/util-probabilities
yt add @lazy-random/util-probabilities
```

## 使用方式 (Usage)

```ts
import { get_prob, get_prob_float } from '@lazy-random/util-probabilities';

get_prob(5, 100);       // [41, 20, 16, 13, 10]
get_prob_float(5, 100); // [40.96, 20, 16, 12.8, 10.24]
```

```ts
import { get_prob, get_prob_float } from '@lazy-random/util-probabilities';
```

> 注意：此套件**沒有**預設導出 (Default Export)，請一律使用具名匯出 (Named Export)。

## API 文件 (API Documentation)

### get_prob(size: number, sum: number): number[]

將 `sum` 拆成 `size` 份的整數 (Integer) 陣列 (Array)。

- 迴圈 (Loop) 依序計算 `Math.round(剩餘分數 / size)` 並累加已分配總額 (Allocated Total)。
- 迴圈執行 `size - 1` 次，最後以 `sum - randomTotal` 算出差額 (Remainder)，用 `unshift()` 放在**陣列開頭**，藉此保證所有元素加總等於 `sum`。
- `size` 為 `1` 時迴圈不執行，直接回傳 `[sum]`。

```ts
get_prob(5, 100); // [41, 20, 16, 13, 10]，加總為 100
```

### get_prob_float(size: number, sum: number): number[]

與 `get_prob()` 相同流程，但不經過四捨五入 (Round)，回傳浮點數 (Float) 陣列；同樣把差額放在第一筆，加總恆等於 `sum`。

```ts
get_prob_float(5, 100); // [40.96, 20, 16, 12.8, 10.24]，加總為 100
```

## 已知限制 (Known Limitations)

- 兩個函式的除數固定為 `size`，但 `score` 逐次遞減，因此各份並非等分 (Equal Split)，而是前大後小的遞減分布；詳見原始碼中的 `TODO` 註解。
- `size <= 0` 時 `while (i--)` 的條件永為真，會造成無窮迴圈 (Infinite Loop)，請勿傳入非正數；詳見原始碼中的 `TODO` 註解。

## 開發 (Development)

此套件為 monorepo 的一部分，原始碼位於 `src/`，編譯產物輸出至 `dist/`。

```bash
pnpm run build
pnpm run test:jest
```

## 變更日誌 (Changelog)

See [CHANGELOG.md](./CHANGELOG.md).

## 相關資源 (Related Resources)

- [Repository](https://github.com/bluelovers/ws-random/tree/master/packages/@lazy-random/util-probabilities)
- [Issues](https://github.com/bluelovers/ws-random/issues)

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