extra-rand 0.1.2 → 0.1.3

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/README.md CHANGED
@@ -30,6 +30,13 @@ function randomIntInclusive(min: number, max: number): number
30
30
 
31
31
  The function returns an integer in the range `[Math.ceil(min), Math.floor(max)]`.
32
32
 
33
+ ### randomByWeight
34
+ ```ts
35
+ function randomByWeight(weights: number[]): number
36
+ ```
37
+
38
+ The function returns an index of one of weights.
39
+
33
40
  ### Linear Map
34
41
  These low-level methods help you to use random number generators other than `Math.random()`.
35
42
 
package/lib/index.d.ts CHANGED
@@ -1,5 +1,6 @@
1
1
  export * from './random';
2
2
  export * from './random-int';
3
3
  export * from './random-int-inclusive';
4
+ export * from './random-by-weight';
4
5
  export * from './map-to-range';
5
6
  export * from './map-to-int-range';
package/lib/index.js CHANGED
@@ -17,6 +17,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
17
17
  __exportStar(require("./random"), exports);
18
18
  __exportStar(require("./random-int"), exports);
19
19
  __exportStar(require("./random-int-inclusive"), exports);
20
+ __exportStar(require("./random-by-weight"), exports);
20
21
  __exportStar(require("./map-to-range"), exports);
21
22
  __exportStar(require("./map-to-int-range"), exports);
22
23
  //# sourceMappingURL=index.js.map
package/lib/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,2CAAwB;AACxB,+CAA4B;AAC5B,yDAAsC;AAEtC,iDAA8B;AAC9B,qDAAkC"}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,2CAAwB;AACxB,+CAA4B;AAC5B,yDAAsC;AACtC,qDAAkC;AAElC,iDAA8B;AAC9B,qDAAkC"}
@@ -0,0 +1 @@
1
+ export declare function randomByWeight(weights: number[]): number;
@@ -0,0 +1,17 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.randomByWeight = void 0;
4
+ const random_1 = require("./random");
5
+ function randomByWeight(weights) {
6
+ const min = 0;
7
+ const max = weights.reduce((acc, cur) => acc + Math.max(cur, 0));
8
+ const randomValue = (0, random_1.random)(min, max);
9
+ for (let i = 0, acc = weights[i]; i < weights.length; acc += weights[++i]) {
10
+ if (randomValue < acc) {
11
+ return i;
12
+ }
13
+ }
14
+ throw new Error('Impossible route');
15
+ }
16
+ exports.randomByWeight = randomByWeight;
17
+ //# sourceMappingURL=random-by-weight.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"random-by-weight.js","sourceRoot":"","sources":["../src/random-by-weight.ts"],"names":[],"mappings":";;;AAAA,qCAAiC;AAEjC,SAAgB,cAAc,CAAC,OAAiB;IAC9C,MAAM,GAAG,GAAG,CAAC,CAAA;IACb,MAAM,GAAG,GAAG,OAAO,CAAC,MAAM,CAAC,CAAC,GAAG,EAAE,GAAG,EAAE,EAAE,CAAC,GAAG,GAAG,IAAI,CAAC,GAAG,CAAC,GAAG,EAAE,CAAC,CAAC,CAAC,CAAA;IAEhE,MAAM,WAAW,GAAG,IAAA,eAAM,EAAC,GAAG,EAAE,GAAG,CAAC,CAAA;IACpC,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,GAAG,GAAG,OAAO,CAAC,CAAC,CAAC,EAAE,CAAC,GAAG,OAAO,CAAC,MAAM,EAAE,GAAG,IAAI,OAAO,CAAC,EAAE,CAAC,CAAC,EAAE;QACzE,IAAI,WAAW,GAAG,GAAG,EAAE;YACrB,OAAO,CAAC,CAAA;SACT;KACF;IAED,MAAM,IAAI,KAAK,CAAC,kBAAkB,CAAC,CAAA;AACrC,CAAC;AAZD,wCAYC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "extra-rand",
3
- "version": "0.1.2",
3
+ "version": "0.1.3",
4
4
  "description": "Yet another random library",
5
5
  "keywords": [
6
6
  "random"
@@ -39,7 +39,6 @@
39
39
  "@typescript-eslint/eslint-plugin": "^5.57.0",
40
40
  "@typescript-eslint/parser": "^5.57.0",
41
41
  "eslint": "^8.37.0",
42
- "extra-generator": "^0.2.17",
43
42
  "husky": "^4.3.8",
44
43
  "jest": "^29.5.0",
45
44
  "npm-run-all": "^4.1.5",