extra-rand 0.1.5 → 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 +5 -13
- package/lib/index.d.ts +0 -1
- package/lib/index.js +0 -1
- package/lib/index.js.map +1 -1
- package/lib/map-to-index-by-weight.js +1 -2
- package/lib/map-to-index-by-weight.js.map +1 -1
- package/lib/random-int-inclusive.js +2 -2
- package/lib/random-int-inclusive.js.map +1 -1
- package/lib/random-int.js +2 -2
- package/lib/random-int.js.map +1 -1
- package/package.json +1 -1
- package/src/index.ts +0 -1
- package/src/map-to-index-by-weight.ts +1 -2
- package/src/random-int-inclusive.ts +3 -3
- package/src/random-int.ts +3 -3
- package/lib/map-to-int-range.d.ts +0 -1
- package/lib/map-to-int-range.js +0 -9
- package/lib/map-to-int-range.js.map +0 -1
- package/src/map-to-int-range.ts +0 -9
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
|
-
###
|
|
41
|
-
These low-level functions 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
|
-
|
|
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
|
-
|
|
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
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,
|
|
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,
|
|
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;
|
|
1
|
+
{"version":3,"file":"map-to-index-by-weight.js","sourceRoot":"","sources":["../src/map-to-index-by-weight.ts"],"names":[],"mappings":";;;AAAA,iDAA2C;AAE3C,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,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"}
|
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.randomIntInclusive = void 0;
|
|
4
|
-
const
|
|
4
|
+
const map_to_range_1 = require("./map-to-range");
|
|
5
5
|
function randomIntInclusive(min, max) {
|
|
6
|
-
return (0,
|
|
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,
|
|
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
|
|
4
|
+
const map_to_range_1 = require("./map-to-range");
|
|
5
5
|
function randomInt(min, max) {
|
|
6
|
-
return (0,
|
|
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
|
package/lib/random-int.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"random-int.js","sourceRoot":"","sources":["../src/random-int.ts"],"names":[],"mappings":";;;AAAA,
|
|
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
package/src/index.ts
CHANGED
|
@@ -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 =
|
|
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
|
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { mapToRange } from './map-to-range'
|
|
2
2
|
|
|
3
3
|
export function randomIntInclusive(min: number, max: number): number {
|
|
4
|
-
return
|
|
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 {
|
|
1
|
+
import { mapToRange } from './map-to-range'
|
|
2
2
|
|
|
3
3
|
export function randomInt(min: number, max: number): number {
|
|
4
|
-
return
|
|
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;
|
package/lib/map-to-int-range.js
DELETED
|
@@ -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"}
|
package/src/map-to-int-range.ts
DELETED