@lazy-random/fake-lib-r-math-rng
v2.0.1
Published
將任意回傳 0~1 亂數 (Random Number) 的函式包裝為 lib-r-math.js 的 IRNG 介面 (Interface),方便把自訂的偽亂數發生器 (PRNG) 接入 R 統計函式
Downloads
267
Maintainers
Readme
@lazy-random/fake-lib-r-math-rng
將任意「回傳 0~1 亂數 (Random Number) 的函式」包裝為 lib-r-math.js 所使用的 IRNG 介面 (Interface),方便把自訂的偽亂數發生器 (PRNG) 接入 R 統計函式的實作。
特色 (Features)
- 一行程式碼即可把
() => number形式的亂數函式轉為IRNG物件 - 同時提供
unif_rand與internal_unif_rand,符合IRNG的介面形狀 (Shape) - 支援批量產生:傳入
n > 1時回傳長度為n的亂數陣列 (Array) - 無執行期 (Runtime) 相依,不需要安裝
lib-r-math.js即可使用
安裝 (Installation)
yarn add @lazy-random/fake-lib-r-math-rng
yarn-tool add @lazy-random/fake-lib-r-math-rng
yt add @lazy-random/fake-lib-r-math-rng使用方式 (Usage)
import fakeLibRMathRng from '@lazy-random/fake-lib-r-math-rng';
// 以任意回傳 0~1 的亂數函式作為底層亂數來源 (Entropy Source)
const rng = fakeLibRMathRng(() => Math.random());
// 不傳參數:取得單一亂數
const value = rng.unif_rand();
// 傳入 n > 1:取得長度為 n 的亂數陣列
const values = rng.unif_rand(5);
// 兩個欄位指向同一個函式,可自由搭配 lib-r-math.js 相關實作的取用方式
rng.internal_unif_rand(3);API 文件 (API Documentation)
fakeLibRMathRng(fn)
| 參數 (Parameter) | 型別 (Type) | 說明 (Description) |
| --- | --- | --- |
| fn | () => number | 底層亂數函式,每次呼叫回傳一個 0~1 的亂數 |
| 回傳值 (Returns) | 型別 (Type) | 說明 (Description) |
| --- | --- | --- |
| IRNG | IRNG | 包裝後的亂數發生器物件,含 unif_rand 與 internal_unif_rand |
unif_rand(n?)
| 參數 (Parameter) | 型別 (Type) | 說明 (Description) |
| --- | --- | --- |
| n | number \| undefined | 要產生的亂數個數;省略或不大於 1 時回傳單一亂數,大於 1 時回傳亂數陣列 |
IRNG型別來自lib-r-math.js,但因為是以型別 (Type) 方式引用,執行期並無此相依。
開發 (Development)
pnpm run build
pnpm run test:jest變更日誌 (Changelog)
詳見 CHANGELOG.md。
