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-distributions

v2.0.1

Published

搭配亂數產生器 (RNG) 使用的分布 (Distribution) 與取值工具函式 (Utility Function),提供區間亂數 (Random Number)、整數取值與隨機索引 (Random Index) 等常用方法

Downloads

559

Readme

@lazy-random/util-distributions

搭配亂數產生器 (RNG) 使用的分布 (Distribution) 與取值工具函式 (Utility Function),提供區間亂數 (Random Number)、整數取值與隨機索引 (Random Index) 等常用方法。

特色 (Features)

  • float():在 [min, max) 區間產生浮點數 (Float)。
  • int():在 [min, max](含端點,Inclusive)區間產生整數 (Integer)。
  • randIndexByLength():依長度產生 0 ~ len - 1 的隨機索引 (Random Index)。
  • randIndexWithRange():在指定起訖範圍內產生隨機索引。
  • 所有函式 (Function) 接收實作 IRNGLike 介面 (Interface) 的亂數產生器 (RNG),方便搭配任何自訂種子 (Seed) 的實作。

安裝 (Installation)

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

使用方式 (Usage)

import { float, int, randIndexByLength, randIndexWithRange } from '@lazy-random/util-distributions';
import type { IRNGLike } from '@lazy-random/rng-abstract';

declare const rng: IRNGLike;

float(rng, 0, 1);            // 0~1 的浮點數 (Float)
int(rng, 1, 6);              // 1~6 的整數 (Integer)
randIndexByLength(rng, 10);          // 0~9 的隨機索引 (Random Index)
randIndexWithRange(rng, 5, 10); // 5~9 的隨機索引

也可以使用預設導出 (Default Export) 的集合物件:

import UtilDistributions from '@lazy-random/util-distributions';

UtilDistributions.int(rng, 0, 100);
UtilDistributions.float(rng, 0, 1);
UtilDistributions.randIndexByLength(rng, 5);

API 文件 (API Documentation)

float(random, min, max): number

回傳 [min, max) 區間內的浮點數 (Float),公式為 random.next() * (max - min) + min。

  • random:實作 IRNGLike(需有 next(): number)的亂數產生器 (RNG)。
  • min:下界 (Lower Bound)。
  • max:上界 (Upper Bound),不含於結果。
  • 回傳:區間內的浮點數。

int(random, min, max): number

回傳 [min, max](含端點,Inclusive)區間內的整數 (Integer)。內部以 randIndexWithRange(random, min, max + 1) 實作,故 max 會被包含。

randIndexByLength(random, len): number

回傳 0 ~ len - 1 的隨機索引 (Random Index),以 Math.floor(random.next() * len) 取得,適合用於陣列 (Array) 取值。

randIndexWithRange(random, start, end): number

回傳 [start, end) 區間取整後的隨機索引,等同 Math.floor(float(random, start, end))。

預設導出 (Default Export)

UtilDistributions 物件彙整 randIndexByLength、float、int 三個函式,可依屬性 (Property) 呼叫。

開發 (Development)

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

pnpm run build
pnpm run test

變更日誌 (Changelog)

See CHANGELOG.md.

相關資源 (Related Resources)