bias-random 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +10 -0
- package/LICENSE +21 -0
- package/README.md +35 -0
- package/dist/index.cjs +1 -0
- package/dist/index.d.cts +20 -0
- package/dist/index.d.ts +20 -0
- package/dist/index.js +1 -0
- package/package.json +61 -0
package/CHANGELOG.md
ADDED
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2024 Reid Moffat
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
# bias-random
|
|
2
|
+
|
|
3
|
+
[](https://www.npmjs.com/package/bias-random)
|
|
4
|
+
[](https://www.npmjs.com/package/bias-random)
|
|
5
|
+
[](https://www.npmjs.com/package/bias-random)
|
|
6
|
+
|
|
7
|
+
Generates biased random numbers with a customizable bias level, direction, and range
|
|
8
|
+
|
|
9
|
+
## 📦 Installation
|
|
10
|
+
|
|
11
|
+
```bash
|
|
12
|
+
npm install bias-random
|
|
13
|
+
|
|
14
|
+
# or
|
|
15
|
+
yarn add install bias-random
|
|
16
|
+
|
|
17
|
+
# or
|
|
18
|
+
pnpm install bias-random
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
## 🚀 Usage
|
|
22
|
+
|
|
23
|
+
```ts
|
|
24
|
+
import biasedRandom from "bias-random";
|
|
25
|
+
|
|
26
|
+
// Default settings:
|
|
27
|
+
// -Bias towards lower numbers
|
|
28
|
+
// -Bias level of 2
|
|
29
|
+
// -Range 0 to 1
|
|
30
|
+
// Average resulting value will be 1/3
|
|
31
|
+
const defaultResult = biasRandom();
|
|
32
|
+
|
|
33
|
+
// Cutomize the parmeters (all parameters are optional, defaulting to the values above)
|
|
34
|
+
const customResult = biasRandom({ upperBias: true, biasLevel: 4, min: 10, max: 1000 });
|
|
35
|
+
```
|
package/dist/index.cjs
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"use strict";var n=Object.defineProperty;var u=Object.getOwnPropertyDescriptor;var i=Object.getOwnPropertyNames;var s=Object.prototype.hasOwnProperty;var m=(a,e)=>{for(var r in e)n(a,r,{get:e[r],enumerable:!0})},l=(a,e,r,t)=>{if(e&&typeof e=="object"||typeof e=="function")for(let o of i(e))!s.call(a,o)&&o!==r&&n(a,o,{get:()=>e[o],enumerable:!(t=u(e,o))||t.enumerable});return a};var b=a=>l(n({},"__esModule",{value:!0}),a);var d={};m(d,{default:()=>f});module.exports=b(d);var p=({upperBias:a=!1,biasLevel:e=2,min:r=0,max:t=1}={})=>{if(typeof e!="number"||e<1)throw new TypeError(`Parameter 'biasLevel' must be a number least 1 (value: ${e}); use upperBias to swap bias direction`);if(typeof r!="number"||typeof t!="number"||r>=t)throw new TypeError(`Parameter 'min' muist be less than 'max' (you can flip them for a valid result). Min value: ${r} Max value: ${t}`);if(typeof a!="boolean")throw new TypeError(`Parameter 'upperBias' must be a boolean, value '${a}' is invalid`);let o=Math.pow(Math.random(),e);return a&&(o=1-o),r+o*(t-r)},f=p;
|
package/dist/index.d.cts
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
interface BiasedRandomOptions {
|
|
2
|
+
upperBias?: boolean;
|
|
3
|
+
biasLevel?: number;
|
|
4
|
+
min?: number;
|
|
5
|
+
max?: number;
|
|
6
|
+
}
|
|
7
|
+
/**
|
|
8
|
+
* Generates a random number, biased towards lower or higher numbers with the level of your choice
|
|
9
|
+
*
|
|
10
|
+
* @param upperBias Set to true to bias this towards higher numbers instead of lower numbers. E.g. with a range of 0
|
|
11
|
+
* to 1, a biasLevel of 2, the average value would be 2/3 (1 - (1 / 3))
|
|
12
|
+
* @param biasLevel How strongly the bias is, default 2. The resulting values have an average value of 1 / (biasLevel
|
|
13
|
+
* + 1) (given upperBias = false and a range of 0 to 1). Can be a decimal number, but must be at least 1 (for less than
|
|
14
|
+
* 1, this is equivalent to using an upperBias, e.g. a bias of 0.5 is the same as an upper bias of 2)
|
|
15
|
+
* @param min Minimum value of the result, default 0. This scales the result, but keeps the relative bias
|
|
16
|
+
* @param max Maximum value of the result, default 1. This scales the result, but keeps the relative bias
|
|
17
|
+
*/
|
|
18
|
+
declare const biasedRandom: ({ upperBias, biasLevel, min, max }?: BiasedRandomOptions) => number;
|
|
19
|
+
|
|
20
|
+
export { biasedRandom as default };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
interface BiasedRandomOptions {
|
|
2
|
+
upperBias?: boolean;
|
|
3
|
+
biasLevel?: number;
|
|
4
|
+
min?: number;
|
|
5
|
+
max?: number;
|
|
6
|
+
}
|
|
7
|
+
/**
|
|
8
|
+
* Generates a random number, biased towards lower or higher numbers with the level of your choice
|
|
9
|
+
*
|
|
10
|
+
* @param upperBias Set to true to bias this towards higher numbers instead of lower numbers. E.g. with a range of 0
|
|
11
|
+
* to 1, a biasLevel of 2, the average value would be 2/3 (1 - (1 / 3))
|
|
12
|
+
* @param biasLevel How strongly the bias is, default 2. The resulting values have an average value of 1 / (biasLevel
|
|
13
|
+
* + 1) (given upperBias = false and a range of 0 to 1). Can be a decimal number, but must be at least 1 (for less than
|
|
14
|
+
* 1, this is equivalent to using an upperBias, e.g. a bias of 0.5 is the same as an upper bias of 2)
|
|
15
|
+
* @param min Minimum value of the result, default 0. This scales the result, but keeps the relative bias
|
|
16
|
+
* @param max Maximum value of the result, default 1. This scales the result, but keeps the relative bias
|
|
17
|
+
*/
|
|
18
|
+
declare const biasedRandom: ({ upperBias, biasLevel, min, max }?: BiasedRandomOptions) => number;
|
|
19
|
+
|
|
20
|
+
export { biasedRandom as default };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
var n=({upperBias:o=!1,biasLevel:a=2,min:e=0,max:r=1}={})=>{if(typeof a!="number"||a<1)throw new TypeError(`Parameter 'biasLevel' must be a number least 1 (value: ${a}); use upperBias to swap bias direction`);if(typeof e!="number"||typeof r!="number"||e>=r)throw new TypeError(`Parameter 'min' muist be less than 'max' (you can flip them for a valid result). Min value: ${e} Max value: ${r}`);if(typeof o!="boolean")throw new TypeError(`Parameter 'upperBias' must be a boolean, value '${o}' is invalid`);let t=Math.pow(Math.random(),a);return o&&(t=1-t),e+t*(r-e)},u=n;export{u as default};
|
package/package.json
ADDED
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "bias-random",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "Generates biased random numbers with a customizable bias level, direction, and range",
|
|
5
|
+
"author": "Reid Moffat <reid.moffat9@gmail.com>",
|
|
6
|
+
"license": "MIT",
|
|
7
|
+
"keywords": [
|
|
8
|
+
"random",
|
|
9
|
+
"bias",
|
|
10
|
+
"biased",
|
|
11
|
+
"random-bias",
|
|
12
|
+
"weighted-random",
|
|
13
|
+
"probability",
|
|
14
|
+
"skewed-random",
|
|
15
|
+
"biased-numbers",
|
|
16
|
+
"random-distribution",
|
|
17
|
+
"random-skew",
|
|
18
|
+
"weighted-numbers",
|
|
19
|
+
"probability-distribution",
|
|
20
|
+
"random-bias-generator",
|
|
21
|
+
"custom-distribution",
|
|
22
|
+
"random-number-generator",
|
|
23
|
+
"rng",
|
|
24
|
+
"biased-probability",
|
|
25
|
+
"skewed-distribution"
|
|
26
|
+
],
|
|
27
|
+
"type": "module",
|
|
28
|
+
"main": "dist/index.js",
|
|
29
|
+
"module": "dist/index.mjs",
|
|
30
|
+
"types": "dist/index.d.ts",
|
|
31
|
+
"scripts": {
|
|
32
|
+
"lint": "tsc",
|
|
33
|
+
"test": "cross-env TS_NODE_PROJECT='./tsconfig.json' mocha --ui tdd",
|
|
34
|
+
"build": "tsup src/index.ts --format cjs,esm --dts --minify",
|
|
35
|
+
"deployHelp": "echo 1) Run 'changeset' 2) Merge changes to main 3) Merge changeset PR 4) npm run deploy (verify it looks good)",
|
|
36
|
+
"deploy": "git checkout main && git pull && npm run build && npm publish"
|
|
37
|
+
},
|
|
38
|
+
"files": [
|
|
39
|
+
"CHANGELOG.md",
|
|
40
|
+
"dist"
|
|
41
|
+
],
|
|
42
|
+
"repository": {
|
|
43
|
+
"type": "git",
|
|
44
|
+
"url": "https://github.com/reid-moffat/bias-random"
|
|
45
|
+
},
|
|
46
|
+
"bugs": "https://github.com/reid-moffat/bias-random/issues",
|
|
47
|
+
"devDependencies": {
|
|
48
|
+
"@changesets/cli": "^2.27.8",
|
|
49
|
+
"@types/chai": "^4.3.19",
|
|
50
|
+
"@types/mocha": "^10.0.8",
|
|
51
|
+
"@types/node": "^22.6.1",
|
|
52
|
+
"chai": "^5.1.1",
|
|
53
|
+
"cross-env": "^7.0.3",
|
|
54
|
+
"generate-arrays": "^2.0.0",
|
|
55
|
+
"mocha": "^10.7.3",
|
|
56
|
+
"suite-metrics": "^1.3.1",
|
|
57
|
+
"ts-node": "^10.9.2",
|
|
58
|
+
"tsup": "^8.3.0",
|
|
59
|
+
"typescript": "^5.6.2"
|
|
60
|
+
}
|
|
61
|
+
}
|