extra-rand 0.1.3 → 0.1.5

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
@@ -37,8 +37,8 @@ function randomByWeight(weights: number[]): number
37
37
 
38
38
  The function returns an index of one of weights.
39
39
 
40
- ### Linear Map
41
- These low-level methods help you to use random number generators other than `Math.random()`.
40
+ ### Map
41
+ These low-level functions help you to use random number generators other than `Math.random()`.
42
42
 
43
43
  #### mapToRange
44
44
  ```ts
@@ -57,3 +57,12 @@ function mapToIntRange(
57
57
  , newMin: number, newMax: number
58
58
  ): number
59
59
  ```
60
+
61
+ #### mapToIndexByWeight
62
+ ```ts
63
+ function mapByWeight(
64
+ value: number
65
+ , oldMin: number, oldMax: number
66
+ , weights: number[]
67
+ ): number
68
+ ```
package/lib/index.d.ts CHANGED
@@ -4,3 +4,4 @@ export * from './random-int-inclusive';
4
4
  export * from './random-by-weight';
5
5
  export * from './map-to-range';
6
6
  export * from './map-to-int-range';
7
+ export * from './map-to-index-by-weight';
package/lib/index.js CHANGED
@@ -20,4 +20,5 @@ __exportStar(require("./random-int-inclusive"), exports);
20
20
  __exportStar(require("./random-by-weight"), exports);
21
21
  __exportStar(require("./map-to-range"), exports);
22
22
  __exportStar(require("./map-to-int-range"), exports);
23
+ __exportStar(require("./map-to-index-by-weight"), exports);
23
24
  //# 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;AACtC,qDAAkC;AAElC,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;AAClC,2DAAwC"}
@@ -0,0 +1 @@
1
+ export declare function mapToIndexByWeight(value: number, oldMin: number, oldMax: number, weights: number[]): number;
@@ -0,0 +1,34 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.mapToIndexByWeight = void 0;
4
+ const map_to_range_1 = require("./map-to-range");
5
+ const map_to_int_range_1 = require("./map-to-int-range");
6
+ function mapToIndexByWeight(value, oldMin, oldMax, weights) {
7
+ const newMin = 0;
8
+ const newMax = weights.reduce((acc, cur) => acc + Math.max(cur, 0));
9
+ if (newMax === 0) {
10
+ const index = (0, map_to_int_range_1.mapToIntRange)(value, oldMin, oldMax, newMin, weights.length);
11
+ return index === weights.length
12
+ ? weights.length - 1
13
+ : index;
14
+ }
15
+ else {
16
+ const newValue = (0, map_to_range_1.mapToRange)(value, oldMin, oldMax, newMin, newMax);
17
+ let remains = newMax;
18
+ for (let i = weights.length; i--;) {
19
+ const weight = weights[i];
20
+ if (weight <= 0) {
21
+ continue;
22
+ }
23
+ else {
24
+ remains -= weight;
25
+ if (newValue >= remains) {
26
+ return i;
27
+ }
28
+ }
29
+ }
30
+ throw new Error('Impossible route');
31
+ }
32
+ }
33
+ exports.mapToIndexByWeight = mapToIndexByWeight;
34
+ //# sourceMappingURL=map-to-index-by-weight.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"map-to-index-by-weight.js","sourceRoot":"","sources":["../src/map-to-index-by-weight.ts"],"names":[],"mappings":";;;AAAA,iDAA2C;AAC3C,yDAAkD;AAElD,SAAgB,kBAAkB,CAChC,KAAa,EACb,MAAc,EAAE,MAAc,EAC9B,OAAiB;IAEjB,MAAM,MAAM,GAAG,CAAC,CAAA;IAChB,MAAM,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC,CAAC,GAAG,EAAE,GAAG,EAAE,EAAE,CAAC,GAAG,GAAG,IAAI,CAAC,GAAG,CAAC,GAAG,EAAE,CAAC,CAAC,CAAC,CAAA;IACnE,IAAI,MAAM,KAAK,CAAC,EAAE;QAEhB,MAAM,KAAK,GAAG,IAAA,gCAAa,EAAC,KAAK,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,EAAE,OAAO,CAAC,MAAM,CAAC,CAAA;QAE1E,OAAO,KAAK,KAAK,OAAO,CAAC,MAAM;YAC1B,CAAC,CAAC,OAAO,CAAC,MAAM,GAAG,CAAC;YACpB,CAAC,CAAC,KAAK,CAAA;KACb;SAAM;QACL,MAAM,QAAQ,GAAG,IAAA,yBAAU,EAAC,KAAK,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,CAAC,CAAA;QAElE,IAAI,OAAO,GAAG,MAAM,CAAA;QACpB,KAAK,IAAI,CAAC,GAAG,OAAO,CAAC,MAAM,EAAE,CAAC,EAAE,GAAG;YACjC,MAAM,MAAM,GAAG,OAAO,CAAC,CAAC,CAAC,CAAA;YACzB,IAAI,MAAM,IAAI,CAAC,EAAE;gBACf,SAAQ;aACT;iBAAM;gBACL,OAAO,IAAI,MAAM,CAAA;gBACjB,IAAI,QAAQ,IAAI,OAAO,EAAE;oBACvB,OAAO,CAAC,CAAA;iBACT;aACF;SACF;QAED,MAAM,IAAI,KAAK,CAAC,kBAAkB,CAAC,CAAA;KACpC;AACH,CAAC;AAhCD,gDAgCC"}
@@ -1,17 +1,9 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.randomByWeight = void 0;
4
- const random_1 = require("./random");
4
+ const map_to_index_by_weight_1 = require("./map-to-index-by-weight");
5
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');
6
+ return (0, map_to_index_by_weight_1.mapToIndexByWeight)(Math.random(), 0, 1, weights);
15
7
  }
16
8
  exports.randomByWeight = randomByWeight;
17
9
  //# sourceMappingURL=random-by-weight.js.map
@@ -1 +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"}
1
+ {"version":3,"file":"random-by-weight.js","sourceRoot":"","sources":["../src/random-by-weight.ts"],"names":[],"mappings":";;;AAAA,qEAA6D;AAE7D,SAAgB,cAAc,CAAC,OAAiB;IAC9C,OAAO,IAAA,2CAAkB,EACvB,IAAI,CAAC,MAAM,EAAE,EACb,CAAC,EAAE,CAAC,EACJ,OAAO,CACR,CAAA;AACH,CAAC;AAND,wCAMC"}
package/package.json CHANGED
@@ -1,12 +1,13 @@
1
1
  {
2
2
  "name": "extra-rand",
3
- "version": "0.1.3",
3
+ "version": "0.1.5",
4
4
  "description": "Yet another random library",
5
5
  "keywords": [
6
6
  "random"
7
7
  ],
8
8
  "files": [
9
- "lib"
9
+ "lib",
10
+ "src"
10
11
  ],
11
12
  "main": "lib/index.js",
12
13
  "types": "lib/index.d.ts",
package/src/index.ts ADDED
@@ -0,0 +1,8 @@
1
+ export * from './random'
2
+ export * from './random-int'
3
+ export * from './random-int-inclusive'
4
+ export * from './random-by-weight'
5
+
6
+ export * from './map-to-range'
7
+ export * from './map-to-int-range'
8
+ export * from './map-to-index-by-weight'
@@ -0,0 +1,36 @@
1
+ import { mapToRange } from './map-to-range'
2
+ import { mapToIntRange } from './map-to-int-range'
3
+
4
+ export function mapToIndexByWeight(
5
+ value: number
6
+ , oldMin: number, oldMax: number
7
+ , weights: number[]
8
+ ): number {
9
+ const newMin = 0
10
+ const newMax = weights.reduce((acc, cur) => acc + Math.max(cur, 0))
11
+ if (newMax === 0) {
12
+ // 只有在所有权重都小于等于0的情况下会进入此路径, 这时将所有权重都视为1.
13
+ const index = mapToIntRange(value, oldMin, oldMax, newMin, weights.length)
14
+
15
+ return index === weights.length
16
+ ? weights.length - 1
17
+ : index
18
+ } else {
19
+ const newValue = mapToRange(value, oldMin, oldMax, newMin, newMax)
20
+
21
+ let remains = newMax
22
+ for (let i = weights.length; i--;) {
23
+ const weight = weights[i]
24
+ if (weight <= 0) {
25
+ continue
26
+ } else {
27
+ remains -= weight
28
+ if (newValue >= remains) {
29
+ return i
30
+ }
31
+ }
32
+ }
33
+
34
+ throw new Error('Impossible route')
35
+ }
36
+ }
@@ -0,0 +1,9 @@
1
+ import { mapToRange } from './map-to-range'
2
+
3
+ export function mapToIntRange(
4
+ value: number
5
+ , oldMin: number, oldMax: number
6
+ , newMin: number, newMax: number
7
+ ): number {
8
+ return Math.floor(mapToRange(value, oldMin, oldMax, newMin, newMax))
9
+ }
@@ -0,0 +1,9 @@
1
+ export function mapToRange(
2
+ value: number
3
+ , oldMin: number, oldMax: number
4
+ , newMin: number, newMax: number
5
+ ): number {
6
+ return (value - oldMin) / (oldMax - oldMin)
7
+ * (newMax - newMin)
8
+ + newMin
9
+ }
@@ -0,0 +1,9 @@
1
+ import { mapToIndexByWeight } from './map-to-index-by-weight'
2
+
3
+ export function randomByWeight(weights: number[]): number {
4
+ return mapToIndexByWeight(
5
+ Math.random()
6
+ , 0, 1
7
+ , weights
8
+ )
9
+ }
@@ -0,0 +1,9 @@
1
+ import { mapToIntRange } from './map-to-int-range'
2
+
3
+ export function randomIntInclusive(min: number, max: number): number {
4
+ return mapToIntRange(
5
+ Math.random()
6
+ , 0, 1
7
+ , Math.ceil(min), Math.floor(max) + 1
8
+ )
9
+ }
@@ -0,0 +1,9 @@
1
+ import { mapToIntRange } from './map-to-int-range'
2
+
3
+ export function randomInt(min: number, max: number): number {
4
+ return mapToIntRange(
5
+ Math.random()
6
+ , 0, 1
7
+ , Math.ceil(min), Math.floor(max)
8
+ )
9
+ }
package/src/random.ts ADDED
@@ -0,0 +1,9 @@
1
+ import { mapToRange } from './map-to-range'
2
+
3
+ export function random(min: number, max: number): number {
4
+ return mapToRange(
5
+ Math.random()
6
+ , 0, 1
7
+ , min, max
8
+ )
9
+ }