extra-rand 0.1.5 → 0.2.1

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,11 @@ 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
+ ### randomBool
34
+ ```ts
35
+ function randomBool(probabilityOfTrue: number): boolean
36
+ ```
37
+
33
38
  ### randomByWeight
34
39
  ```ts
35
40
  function randomByWeight(weights: number[]): number
@@ -37,10 +42,7 @@ function randomByWeight(weights: number[]): number
37
42
 
38
43
  The function returns an index of one of weights.
39
44
 
40
- ### Map
41
- These low-level functions help you to use random number generators other than `Math.random()`.
42
-
43
- #### mapToRange
45
+ ### mapToRange
44
46
  ```ts
45
47
  function mapToRange(
46
48
  value: number
@@ -49,16 +51,9 @@ function mapToRange(
49
51
  ): number
50
52
  ```
51
53
 
52
- #### mapToIntRange
53
- ```ts
54
- function mapToIntRange(
55
- value: number
56
- , oldMin: number, oldMax: number
57
- , newMin: number, newMax: number
58
- ): number
59
- ```
54
+ A low-level function helps you to use random number generators other than `Math.random()`.
60
55
 
61
- #### mapToIndexByWeight
56
+ ### mapToIndexByWeight
62
57
  ```ts
63
58
  function mapByWeight(
64
59
  value: number
@@ -66,3 +61,5 @@ function mapByWeight(
66
61
  , weights: number[]
67
62
  ): number
68
63
  ```
64
+
65
+ A low-level function helps you to use random number generators other than `Math.random()`.
package/lib/index.d.ts CHANGED
@@ -1,7 +1,7 @@
1
1
  export * from './random';
2
2
  export * from './random-int';
3
3
  export * from './random-int-inclusive';
4
+ export * from './random-bool';
4
5
  export * from './random-by-weight';
5
6
  export * from './map-to-range';
6
- export * from './map-to-int-range';
7
7
  export * from './map-to-index-by-weight';
package/lib/index.js CHANGED
@@ -17,8 +17,8 @@ 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-bool"), exports);
20
21
  __exportStar(require("./random-by-weight"), exports);
21
22
  __exportStar(require("./map-to-range"), exports);
22
- __exportStar(require("./map-to-int-range"), exports);
23
23
  __exportStar(require("./map-to-index-by-weight"), exports);
24
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;AAClC,2DAAwC"}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,2CAAwB;AACxB,+CAA4B;AAC5B,yDAAsC;AACtC,gDAA6B;AAC7B,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,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AAM,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,IAAI,CAAC,KAAK,CAAC,IAAA,yBAAU,EAAC,KAAK,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,EAAE,OAAO,CAAC,MAAM,CAAC,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"}
@@ -0,0 +1 @@
1
+ export declare function randomBool(probabilityOfTrue: number): boolean;
@@ -0,0 +1,9 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.randomBool = void 0;
4
+ const random_1 = require("./random");
5
+ function randomBool(probabilityOfTrue) {
6
+ return (0, random_1.random)(0, 1) < probabilityOfTrue;
7
+ }
8
+ exports.randomBool = randomBool;
9
+ //# sourceMappingURL=random-bool.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"random-bool.js","sourceRoot":"","sources":["../src/random-bool.ts"],"names":[],"mappings":";;;AAAA,qCAAiC;AAEjC,SAAgB,UAAU,CAAC,iBAAyB;IAClD,OAAO,IAAA,eAAM,EAAC,CAAC,EAAE,CAAC,CAAC,GAAG,iBAAiB,CAAA;AACzC,CAAC;AAFD,gCAEC"}
@@ -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,6 +1,6 @@
1
1
  {
2
2
  "name": "extra-rand",
3
- "version": "0.1.5",
3
+ "version": "0.2.1",
4
4
  "description": "Yet another random library",
5
5
  "keywords": [
6
6
  "random"
package/src/index.ts CHANGED
@@ -1,8 +1,8 @@
1
1
  export * from './random'
2
2
  export * from './random-int'
3
3
  export * from './random-int-inclusive'
4
+ export * from './random-bool'
4
5
  export * from './random-by-weight'
5
6
 
6
7
  export * from './map-to-range'
7
- export * from './map-to-int-range'
8
8
  export * from './map-to-index-by-weight'
@@ -1,5 +1,4 @@
1
1
  import { mapToRange } from './map-to-range'
2
- import { mapToIntRange } from './map-to-int-range'
3
2
 
4
3
  export function mapToIndexByWeight(
5
4
  value: number
@@ -10,7 +9,7 @@ export function mapToIndexByWeight(
10
9
  const newMax = weights.reduce((acc, cur) => acc + Math.max(cur, 0))
11
10
  if (newMax === 0) {
12
11
  // 只有在所有权重都小于等于0的情况下会进入此路径, 这时将所有权重都视为1.
13
- const index = mapToIntRange(value, oldMin, oldMax, newMin, weights.length)
12
+ const index = Math.floor(mapToRange(value, oldMin, oldMax, newMin, weights.length))
14
13
 
15
14
  return index === weights.length
16
15
  ? weights.length - 1
@@ -0,0 +1,5 @@
1
+ import { random } from './random'
2
+
3
+ export function randomBool(probabilityOfTrue: number): boolean {
4
+ return random(0, 1) < probabilityOfTrue
5
+ }
@@ -1,9 +1,9 @@
1
- import { mapToIntRange } from './map-to-int-range'
1
+ import { mapToRange } from './map-to-range'
2
2
 
3
3
  export function randomIntInclusive(min: number, max: number): number {
4
- return mapToIntRange(
4
+ return Math.floor(mapToRange(
5
5
  Math.random()
6
6
  , 0, 1
7
7
  , Math.ceil(min), Math.floor(max) + 1
8
- )
8
+ ))
9
9
  }
package/src/random-int.ts CHANGED
@@ -1,9 +1,9 @@
1
- import { mapToIntRange } from './map-to-int-range'
1
+ import { mapToRange } from './map-to-range'
2
2
 
3
3
  export function randomInt(min: number, max: number): number {
4
- return mapToIntRange(
4
+ return Math.floor(mapToRange(
5
5
  Math.random()
6
6
  , 0, 1
7
7
  , Math.ceil(min), Math.floor(max)
8
- )
8
+ ))
9
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"}
@@ -1,9 +0,0 @@
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
- }