npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

@lazy-random/util-probabilities

v2.0.1

Published

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

Readme

@lazy-random/util-probabilities

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

特色 (Features)

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

安裝 (Installation)

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

使用方式 (Usage)

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]
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]。
get_prob(5, 100); // [41, 20, 16, 13, 10],加總為 100

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

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

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/。

pnpm run build
pnpm run test:jest

變更日誌 (Changelog)

See CHANGELOG.md.

相關資源 (Related Resources)