extra-rand 0.1.4 → 0.2.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/README.md CHANGED
@@ -37,10 +37,7 @@ function randomByWeight(weights: number[]): number
37
37
 
38
38
  The function returns an index of one of weights.
39
39
 
40
- ### Map
41
- These low-level methods help you to use random number generators other than `Math.random()`.
42
-
43
- #### mapToRange
40
+ ### mapToRange
44
41
  ```ts
45
42
  function mapToRange(
46
43
  value: number
@@ -49,16 +46,9 @@ function mapToRange(
49
46
  ): number
50
47
  ```
51
48
 
52
- #### mapToIntRange
53
- ```ts
54
- function mapToIntRange(
55
- value: number
56
- , oldMin: number, oldMax: number
57
- , newMin: number, newMax: number
58
- ): number
59
- ```
49
+ A low-level function helps you to use random number generators other than `Math.random()`.
60
50
 
61
- #### mapToIndexByWeight
51
+ ### mapToIndexByWeight
62
52
  ```ts
63
53
  function mapByWeight(
64
54
  value: number
@@ -66,3 +56,5 @@ function mapByWeight(
66
56
  , weights: number[]
67
57
  ): number
68
58
  ```
59
+
60
+ A low-level function helps you to use random number generators other than `Math.random()`.
package/lib/index.d.ts CHANGED
@@ -3,5 +3,4 @@ export * from './random-int';
3
3
  export * from './random-int-inclusive';
4
4
  export * from './random-by-weight';
5
5
  export * from './map-to-range';
6
- export * from './map-to-int-range';
7
6
  export * from './map-to-index-by-weight';
package/lib/index.js CHANGED
@@ -19,6 +19,5 @@ __exportStar(require("./random-int"), exports);
19
19
  __exportStar(require("./random-int-inclusive"), exports);
20
20
  __exportStar(require("./random-by-weight"), exports);
21
21
  __exportStar(require("./map-to-range"), exports);
22
- __exportStar(require("./map-to-int-range"), exports);
23
22
  __exportStar(require("./map-to-index-by-weight"), exports);
24
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;AACtC,qDAAkC;AAElC,iDAA8B;AAC9B,qDAAkC;AAClC,2DAAwC"}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,2CAAwB;AACxB,+CAA4B;AAC5B,yDAAsC;AACtC,qDAAkC;AAElC,iDAA8B;AAC9B,2DAAwC"}
@@ -2,12 +2,11 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.mapToIndexByWeight = void 0;
4
4
  const map_to_range_1 = require("./map-to-range");
5
- const map_to_int_range_1 = require("./map-to-int-range");
6
5
  function mapToIndexByWeight(value, oldMin, oldMax, weights) {
7
6
  const newMin = 0;
8
7
  const newMax = weights.reduce((acc, cur) => acc + Math.max(cur, 0));
9
8
  if (newMax === 0) {
10
- const index = (0, map_to_int_range_1.mapToIntRange)(value, oldMin, oldMax, newMin, weights.length);
9
+ const index = Math.floor((0, map_to_range_1.mapToRange)(value, oldMin, oldMax, newMin, weights.length));
11
10
  return index === weights.length
12
11
  ? weights.length - 1
13
12
  : index;
@@ -1 +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,C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CAAC,CAAA;QAEnF,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,9 +1,9 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.randomIntInclusive = void 0;
4
- const map_to_int_range_1 = require("./map-to-int-range");
4
+ const map_to_range_1 = require("./map-to-range");
5
5
  function randomIntInclusive(min, max) {
6
- return (0, map_to_int_range_1.mapToIntRange)(Math.random(), 0, 1, Math.ceil(min), Math.floor(max) + 1);
6
+ return Math.floor((0, map_to_range_1.mapToRange)(Math.random(), 0, 1, Math.ceil(min), Math.floor(max) + 1));
7
7
  }
8
8
  exports.randomIntInclusive = randomIntInclusive;
9
9
  //# sourceMappingURL=random-int-inclusive.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"random-int-inclusive.js","sourceRoot":"","sources":["../src/random-int-inclusive.ts"],"names":[],"mappings":";;;AAAA,yDAAkD;AAElD,SAAgB,kBAAkB,CAAC,GAAW,EAAE,GAAW;IACzD,OAAO,IAAA,gCAAa,EAClB,IAAI,CAAC,MAAM,EAAE,EACb,CAAC,EAAE,CAAC,EACJ,IAAI,CAAC,IAAI,CAAC,GAAG,CAAC,EAAE,IAAI,CAAC,KAAK,CAAC,GAAG,CAAC,GAAG,CAAC,CACpC,CAAA;AACH,CAAC;AAND,gDAMC"}
1
+ {"version":3,"file":"random-int-inclusive.js","sourceRoot":"","sources":["../src/random-int-inclusive.ts"],"names":[],"mappings":";;;AAAA,iDAA2C;AAE3C,SAAgB,kBAAkB,CAAC,GAAW,EAAE,GAAW;IACzD,OAAO,IAAI,CAAC,KAAK,CAAC,IAAA,yBAAU,EAC1B,IAAI,CAAC,MAAM,EAAE,EACb,CAAC,EAAE,CAAC,EACJ,IAAI,CAAC,IAAI,CAAC,GAAG,CAAC,EAAE,IAAI,CAAC,KAAK,CAAC,GAAG,CAAC,GAAG,CAAC,CACpC,CAAC,CAAA;AACJ,CAAC;AAND,gDAMC"}
package/lib/random-int.js CHANGED
@@ -1,9 +1,9 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.randomInt = void 0;
4
- const map_to_int_range_1 = require("./map-to-int-range");
4
+ const map_to_range_1 = require("./map-to-range");
5
5
  function randomInt(min, max) {
6
- return (0, map_to_int_range_1.mapToIntRange)(Math.random(), 0, 1, Math.ceil(min), Math.floor(max));
6
+ return Math.floor((0, map_to_range_1.mapToRange)(Math.random(), 0, 1, Math.ceil(min), Math.floor(max)));
7
7
  }
8
8
  exports.randomInt = randomInt;
9
9
  //# sourceMappingURL=random-int.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"random-int.js","sourceRoot":"","sources":["../src/random-int.ts"],"names":[],"mappings":";;;AAAA,yDAAkD;AAElD,SAAgB,SAAS,CAAC,GAAW,EAAE,GAAW;IAChD,OAAO,IAAA,gCAAa,EAClB,IAAI,CAAC,MAAM,EAAE,EACb,CAAC,EAAE,CAAC,EACJ,IAAI,CAAC,IAAI,CAAC,GAAG,CAAC,EAAE,IAAI,CAAC,KAAK,CAAC,GAAG,CAAC,CAChC,CAAA;AACH,CAAC;AAND,8BAMC"}
1
+ {"version":3,"file":"random-int.js","sourceRoot":"","sources":["../src/random-int.ts"],"names":[],"mappings":";;;AAAA,iDAA2C;AAE3C,SAAgB,SAAS,CAAC,GAAW,EAAE,GAAW;IAChD,OAAO,IAAI,CAAC,KAAK,CAAC,IAAA,yBAAU,EAC1B,IAAI,CAAC,MAAM,EAAE,EACb,CAAC,EAAE,CAAC,EACJ,IAAI,CAAC,IAAI,CAAC,GAAG,CAAC,EAAE,IAAI,CAAC,KAAK,CAAC,GAAG,CAAC,CAChC,CAAC,CAAA;AACJ,CAAC;AAND,8BAMC"}
package/package.json CHANGED
@@ -1,12 +1,13 @@
1
1
  {
2
2
  "name": "extra-rand",
3
- "version": "0.1.4",
3
+ "version": "0.2.0",
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,7 @@
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-index-by-weight'
@@ -0,0 +1,35 @@
1
+ import { mapToRange } from './map-to-range'
2
+
3
+ export function mapToIndexByWeight(
4
+ value: number
5
+ , oldMin: number, oldMax: number
6
+ , weights: number[]
7
+ ): number {
8
+ const newMin = 0
9
+ const newMax = weights.reduce((acc, cur) => acc + Math.max(cur, 0))
10
+ if (newMax === 0) {
11
+ // 只有在所有权重都小于等于0的情况下会进入此路径, 这时将所有权重都视为1.
12
+ const index = Math.floor(mapToRange(value, oldMin, oldMax, newMin, weights.length))
13
+
14
+ return index === weights.length
15
+ ? weights.length - 1
16
+ : index
17
+ } else {
18
+ const newValue = mapToRange(value, oldMin, oldMax, newMin, newMax)
19
+
20
+ let remains = newMax
21
+ for (let i = weights.length; i--;) {
22
+ const weight = weights[i]
23
+ if (weight <= 0) {
24
+ continue
25
+ } else {
26
+ remains -= weight
27
+ if (newValue >= remains) {
28
+ return i
29
+ }
30
+ }
31
+ }
32
+
33
+ throw new Error('Impossible route')
34
+ }
35
+ }
@@ -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 { mapToRange } from './map-to-range'
2
+
3
+ export function randomIntInclusive(min: number, max: number): number {
4
+ return Math.floor(mapToRange(
5
+ Math.random()
6
+ , 0, 1
7
+ , Math.ceil(min), Math.floor(max) + 1
8
+ ))
9
+ }
@@ -0,0 +1,9 @@
1
+ import { mapToRange } from './map-to-range'
2
+
3
+ export function randomInt(min: number, max: number): number {
4
+ return Math.floor(mapToRange(
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
+ }
@@ -1 +0,0 @@
1
- export declare function mapToIntRange(value: number, oldMin: number, oldMax: number, newMin: number, newMax: number): number;
@@ -1,9 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.mapToIntRange = void 0;
4
- const map_to_range_1 = require("./map-to-range");
5
- function mapToIntRange(value, oldMin, oldMax, newMin, newMax) {
6
- return Math.floor((0, map_to_range_1.mapToRange)(value, oldMin, oldMax, newMin, newMax));
7
- }
8
- exports.mapToIntRange = mapToIntRange;
9
- //# sourceMappingURL=map-to-int-range.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"map-to-int-range.js","sourceRoot":"","sources":["../src/map-to-int-range.ts"],"names":[],"mappings":";;;AAAA,iDAA2C;AAE3C,SAAgB,aAAa,CAC3B,KAAa,EACb,MAAc,EAAE,MAAc,EAC9B,MAAc,EAAE,MAAc;IAE9B,OAAO,IAAI,CAAC,KAAK,CAAC,IAAA,yBAAU,EAAC,KAAK,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,CAAC,CAAC,CAAA;AACtE,CAAC;AAND,sCAMC"}