extra-rand 0.2.0 → 0.3.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 +52 -4
- package/lib/index.d.ts +10 -6
- package/lib/index.js +10 -22
- package/lib/index.js.map +1 -1
- package/lib/map-to-index-by-weight.d.ts +2 -1
- package/lib/map-to-index-by-weight.js +4 -8
- package/lib/map-to-index-by-weight.js.map +1 -1
- package/lib/map-to-range.js +1 -5
- package/lib/map-to-range.js.map +1 -1
- package/lib/random-bool.d.ts +1 -0
- package/lib/random-bool.js +5 -0
- package/lib/random-bool.js.map +1 -0
- package/lib/random-by-model.d.ts +15 -0
- package/lib/random-by-model.js +32 -0
- package/lib/random-by-model.js.map +1 -0
- package/lib/random-by-weight-model.d.ts +6 -0
- package/lib/random-by-weight-model.js +6 -0
- package/lib/random-by-weight-model.js.map +1 -0
- package/lib/random-index-by-weight.d.ts +2 -0
- package/lib/random-index-by-weight.js +5 -0
- package/lib/random-index-by-weight.js.map +1 -0
- package/lib/random-int-inclusive.js +3 -7
- package/lib/random-int-inclusive.js.map +1 -1
- package/lib/random-int.js +3 -7
- package/lib/random-int.js.map +1 -1
- package/lib/random-weighted.d.ts +5 -0
- package/lib/random-weighted.js +7 -0
- package/lib/random-weighted.js.map +1 -0
- package/lib/random.js +3 -7
- package/lib/random.js.map +1 -1
- package/package.json +29 -23
- package/src/index.ts +10 -6
- package/src/map-to-index-by-weight.ts +3 -2
- package/src/random-bool.ts +5 -0
- package/src/random-by-model.ts +43 -0
- package/src/random-by-weight-model.ts +12 -0
- package/src/random-index-by-weight.ts +10 -0
- package/src/random-int-inclusive.ts +1 -1
- package/src/random-int.ts +1 -1
- package/src/random-weighted.ts +12 -0
- package/src/random.ts +1 -1
- package/lib/random-by-weight.d.ts +0 -1
- package/lib/random-by-weight.js +0 -9
- package/lib/random-by-weight.js.map +0 -1
- package/src/random-by-weight.ts +0 -9
package/README.md
CHANGED
|
@@ -30,13 +30,61 @@ 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
|
-
###
|
|
33
|
+
### randomBool
|
|
34
34
|
```ts
|
|
35
|
-
function
|
|
35
|
+
function randomBool(probabilityOfTrue: number): boolean
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
### randomIndexByWeight
|
|
39
|
+
```ts
|
|
40
|
+
function randomIndexByWeight(weights: NonEmptyArray<number>): number
|
|
36
41
|
```
|
|
37
42
|
|
|
38
43
|
The function returns an index of one of weights.
|
|
39
44
|
|
|
45
|
+
### randomWeighted
|
|
46
|
+
```ts
|
|
47
|
+
interface IWeighted {
|
|
48
|
+
weight: number
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
function randomWeighted<T extends IWeighted>(values: NonEmptyArray<T>): T
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
### randomByWeightModel
|
|
55
|
+
```ts
|
|
56
|
+
type IWeightModel<T> = NonEmptyArray<{
|
|
57
|
+
weight: number
|
|
58
|
+
value: T
|
|
59
|
+
}>
|
|
60
|
+
|
|
61
|
+
function randomByWeightModel<T>(model: IWeightModel<T>): number
|
|
62
|
+
```
|
|
63
|
+
|
|
64
|
+
### randomByModel
|
|
65
|
+
```ts
|
|
66
|
+
enum Type {
|
|
67
|
+
Float
|
|
68
|
+
, Integer
|
|
69
|
+
, IntegerInclusive
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
type IRandomModel =
|
|
73
|
+
| number
|
|
74
|
+
| Getter<number>
|
|
75
|
+
| {
|
|
76
|
+
type: Type
|
|
77
|
+
min: number
|
|
78
|
+
max: number
|
|
79
|
+
}
|
|
80
|
+
| NonEmptyArray<{
|
|
81
|
+
weight: number
|
|
82
|
+
value: IRandomModel
|
|
83
|
+
}>
|
|
84
|
+
|
|
85
|
+
function randomByModel(model: IRandomModel): number
|
|
86
|
+
```
|
|
87
|
+
|
|
40
88
|
### mapToRange
|
|
41
89
|
```ts
|
|
42
90
|
function mapToRange(
|
|
@@ -50,10 +98,10 @@ A low-level function helps you to use random number generators other than `Math.
|
|
|
50
98
|
|
|
51
99
|
### mapToIndexByWeight
|
|
52
100
|
```ts
|
|
53
|
-
function
|
|
101
|
+
function mapToIndexByWeight(
|
|
54
102
|
value: number
|
|
55
103
|
, oldMin: number, oldMax: number
|
|
56
|
-
, weights: number
|
|
104
|
+
, weights: NonEmptyArray<number>
|
|
57
105
|
): number
|
|
58
106
|
```
|
|
59
107
|
|
package/lib/index.d.ts
CHANGED
|
@@ -1,6 +1,10 @@
|
|
|
1
|
-
export * from './random';
|
|
2
|
-
export * from './random-int';
|
|
3
|
-
export * from './random-int-inclusive';
|
|
4
|
-
export * from './random-
|
|
5
|
-
export * from './
|
|
6
|
-
export * from './
|
|
1
|
+
export * from './random.js';
|
|
2
|
+
export * from './random-int.js';
|
|
3
|
+
export * from './random-int-inclusive.js';
|
|
4
|
+
export * from './random-bool.js';
|
|
5
|
+
export * from './random-index-by-weight.js';
|
|
6
|
+
export * from './random-weighted.js';
|
|
7
|
+
export * from './random-by-weight-model.js';
|
|
8
|
+
export * from './random-by-model.js';
|
|
9
|
+
export * from './map-to-range.js';
|
|
10
|
+
export * from './map-to-index-by-weight.js';
|
package/lib/index.js
CHANGED
|
@@ -1,23 +1,11 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
o[k2] = m[k];
|
|
12
|
-
}));
|
|
13
|
-
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
-
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
-
};
|
|
16
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
-
__exportStar(require("./random"), exports);
|
|
18
|
-
__exportStar(require("./random-int"), exports);
|
|
19
|
-
__exportStar(require("./random-int-inclusive"), exports);
|
|
20
|
-
__exportStar(require("./random-by-weight"), exports);
|
|
21
|
-
__exportStar(require("./map-to-range"), exports);
|
|
22
|
-
__exportStar(require("./map-to-index-by-weight"), exports);
|
|
1
|
+
export * from './random.js';
|
|
2
|
+
export * from './random-int.js';
|
|
3
|
+
export * from './random-int-inclusive.js';
|
|
4
|
+
export * from './random-bool.js';
|
|
5
|
+
export * from './random-index-by-weight.js';
|
|
6
|
+
export * from './random-weighted.js';
|
|
7
|
+
export * from './random-by-weight-model.js';
|
|
8
|
+
export * from './random-by-model.js';
|
|
9
|
+
export * from './map-to-range.js';
|
|
10
|
+
export * from './map-to-index-by-weight.js';
|
|
23
11
|
//# 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":"
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,aAAa,CAAA;AAC3B,cAAc,iBAAiB,CAAA;AAC/B,cAAc,2BAA2B,CAAA;AACzC,cAAc,kBAAkB,CAAA;AAChC,cAAc,6BAA6B,CAAA;AAC3C,cAAc,sBAAsB,CAAA;AACpC,cAAc,6BAA6B,CAAA;AAC3C,cAAc,sBAAsB,CAAA;AAEpC,cAAc,mBAAmB,CAAA;AACjC,cAAc,6BAA6B,CAAA"}
|
|
@@ -1 +1,2 @@
|
|
|
1
|
-
|
|
1
|
+
import { NonEmptyArray } from 'justypes';
|
|
2
|
+
export declare function mapToIndexByWeight(value: number, oldMin: number, oldMax: number, weights: NonEmptyArray<number>): number;
|
|
@@ -1,18 +1,15 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
exports.mapToIndexByWeight = void 0;
|
|
4
|
-
const map_to_range_1 = require("./map-to-range");
|
|
5
|
-
function mapToIndexByWeight(value, oldMin, oldMax, weights) {
|
|
1
|
+
import { mapToRange } from './map-to-range.js';
|
|
2
|
+
export function mapToIndexByWeight(value, oldMin, oldMax, weights) {
|
|
6
3
|
const newMin = 0;
|
|
7
4
|
const newMax = weights.reduce((acc, cur) => acc + Math.max(cur, 0));
|
|
8
5
|
if (newMax === 0) {
|
|
9
|
-
const index = Math.floor(
|
|
6
|
+
const index = Math.floor(mapToRange(value, oldMin, oldMax, newMin, weights.length));
|
|
10
7
|
return index === weights.length
|
|
11
8
|
? weights.length - 1
|
|
12
9
|
: index;
|
|
13
10
|
}
|
|
14
11
|
else {
|
|
15
|
-
const newValue =
|
|
12
|
+
const newValue = mapToRange(value, oldMin, oldMax, newMin, newMax);
|
|
16
13
|
let remains = newMax;
|
|
17
14
|
for (let i = weights.length; i--;) {
|
|
18
15
|
const weight = weights[i];
|
|
@@ -29,5 +26,4 @@ function mapToIndexByWeight(value, oldMin, oldMax, weights) {
|
|
|
29
26
|
throw new Error('Impossible route');
|
|
30
27
|
}
|
|
31
28
|
}
|
|
32
|
-
exports.mapToIndexByWeight = mapToIndexByWeight;
|
|
33
29
|
//# sourceMappingURL=map-to-index-by-weight.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"map-to-index-by-weight.js","sourceRoot":"","sources":["../src/map-to-index-by-weight.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"map-to-index-by-weight.js","sourceRoot":"","sources":["../src/map-to-index-by-weight.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,UAAU,EAAE,MAAM,mBAAmB,CAAA;AAE9C,MAAM,UAAU,kBAAkB,CAChC,KAAa,EACb,MAAc,EAAE,MAAc,EAC9B,OAA8B;IAE9B,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,CAAC;QAEjB,MAAM,KAAK,GAAG,IAAI,CAAC,KAAK,CAAC,UAAU,CAAC,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;IACd,CAAC;SAAM,CAAC;QACN,MAAM,QAAQ,GAAG,UAAU,CAAC,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,CAAC;YAClC,MAAM,MAAM,GAAG,OAAO,CAAC,CAAC,CAAC,CAAA;YACzB,IAAI,MAAM,IAAI,CAAC,EAAE,CAAC;gBAChB,SAAQ;YACV,CAAC;iBAAM,CAAC;gBACN,OAAO,IAAI,MAAM,CAAA;gBACjB,IAAI,QAAQ,IAAI,OAAO,EAAE,CAAC;oBACxB,OAAO,CAAC,CAAA;gBACV,CAAC;YACH,CAAC;QACH,CAAC;QAED,MAAM,IAAI,KAAK,CAAC,kBAAkB,CAAC,CAAA;IACrC,CAAC;AACH,CAAC"}
|
package/lib/map-to-range.js
CHANGED
|
@@ -1,10 +1,6 @@
|
|
|
1
|
-
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.mapToRange = void 0;
|
|
4
|
-
function mapToRange(value, oldMin, oldMax, newMin, newMax) {
|
|
1
|
+
export function mapToRange(value, oldMin, oldMax, newMin, newMax) {
|
|
5
2
|
return (value - oldMin) / (oldMax - oldMin)
|
|
6
3
|
* (newMax - newMin)
|
|
7
4
|
+ newMin;
|
|
8
5
|
}
|
|
9
|
-
exports.mapToRange = mapToRange;
|
|
10
6
|
//# sourceMappingURL=map-to-range.js.map
|
package/lib/map-to-range.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"map-to-range.js","sourceRoot":"","sources":["../src/map-to-range.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"map-to-range.js","sourceRoot":"","sources":["../src/map-to-range.ts"],"names":[],"mappings":"AAAA,MAAM,UAAU,UAAU,CACxB,KAAa,EACb,MAAc,EAAE,MAAc,EAC9B,MAAc,EAAE,MAAc;IAE9B,OAAO,CAAC,KAAK,GAAG,MAAM,CAAC,GAAG,CAAC,MAAM,GAAG,MAAM,CAAC;UACpC,CAAC,MAAM,GAAG,MAAM,CAAC;UACjB,MAAM,CAAA;AACf,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function randomBool(probabilityOfTrue: number): boolean;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"random-bool.js","sourceRoot":"","sources":["../src/random-bool.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,aAAa,CAAA;AAEpC,MAAM,UAAU,UAAU,CAAC,iBAAyB;IAClD,OAAO,MAAM,CAAC,CAAC,EAAE,CAAC,CAAC,GAAG,iBAAiB,CAAA;AACzC,CAAC"}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { Getter, NonEmptyArray } from 'justypes';
|
|
2
|
+
export declare enum Type {
|
|
3
|
+
Float = 0,
|
|
4
|
+
Integer = 1,
|
|
5
|
+
IntegerInclusive = 2
|
|
6
|
+
}
|
|
7
|
+
export type IRandomModel = number | Getter<number> | {
|
|
8
|
+
type: Type;
|
|
9
|
+
min: number;
|
|
10
|
+
max: number;
|
|
11
|
+
} | NonEmptyArray<{
|
|
12
|
+
weight: number;
|
|
13
|
+
value: IRandomModel;
|
|
14
|
+
}>;
|
|
15
|
+
export declare function randomByModel(model: IRandomModel): number;
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import { isArray, isFunction, isNumber } from 'extra-utils';
|
|
2
|
+
import { randomByWeightModel } from './random-by-weight-model.js';
|
|
3
|
+
import { random } from './random.js';
|
|
4
|
+
import { randomInt } from './random-int.js';
|
|
5
|
+
import { randomIntInclusive } from './random-int-inclusive.js';
|
|
6
|
+
export var Type;
|
|
7
|
+
(function (Type) {
|
|
8
|
+
Type[Type["Float"] = 0] = "Float";
|
|
9
|
+
Type[Type["Integer"] = 1] = "Integer";
|
|
10
|
+
Type[Type["IntegerInclusive"] = 2] = "IntegerInclusive";
|
|
11
|
+
})(Type || (Type = {}));
|
|
12
|
+
export function randomByModel(model) {
|
|
13
|
+
if (isNumber(model)) {
|
|
14
|
+
return model;
|
|
15
|
+
}
|
|
16
|
+
else if (isFunction(model)) {
|
|
17
|
+
return model();
|
|
18
|
+
}
|
|
19
|
+
else if (isArray(model)) {
|
|
20
|
+
const subModel = randomByWeightModel(model);
|
|
21
|
+
return randomByModel(subModel);
|
|
22
|
+
}
|
|
23
|
+
else {
|
|
24
|
+
const { type, min, max } = model;
|
|
25
|
+
switch (type) {
|
|
26
|
+
case Type.Float: return random(min, max);
|
|
27
|
+
case Type.Integer: return randomInt(min, max);
|
|
28
|
+
case Type.IntegerInclusive: return randomIntInclusive(min, max);
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
//# sourceMappingURL=random-by-model.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"random-by-model.js","sourceRoot":"","sources":["../src/random-by-model.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,OAAO,EAAE,UAAU,EAAE,QAAQ,EAAE,MAAM,aAAa,CAAA;AAC3D,OAAO,EAAE,mBAAmB,EAAE,MAAM,6BAA6B,CAAA;AACjE,OAAO,EAAE,MAAM,EAAE,MAAM,aAAa,CAAA;AACpC,OAAO,EAAE,SAAS,EAAE,MAAM,iBAAiB,CAAA;AAC3C,OAAO,EAAE,kBAAkB,EAAE,MAAM,2BAA2B,CAAA;AAE9D,MAAM,CAAN,IAAY,IAIX;AAJD,WAAY,IAAI;IACd,iCAAK,CAAA;IACL,qCAAO,CAAA;IACP,uDAAgB,CAAA;AAClB,CAAC,EAJW,IAAI,KAAJ,IAAI,QAIf;AAeD,MAAM,UAAU,aAAa,CAAC,KAAmB;IAC/C,IAAI,QAAQ,CAAC,KAAK,CAAC,EAAE,CAAC;QACpB,OAAO,KAAK,CAAA;IACd,CAAC;SAAM,IAAI,UAAU,CAAC,KAAK,CAAC,EAAE,CAAC;QAC7B,OAAO,KAAK,EAAE,CAAA;IAChB,CAAC;SAAM,IAAI,OAAO,CAAC,KAAK,CAAC,EAAE,CAAC;QAC1B,MAAM,QAAQ,GAAG,mBAAmB,CAAC,KAAK,CAAC,CAAA;QAC3C,OAAO,aAAa,CAAC,QAAQ,CAAC,CAAA;IAChC,CAAC;SAAM,CAAC;QACN,MAAM,EAAE,IAAI,EAAE,GAAG,EAAE,GAAG,EAAE,GAAG,KAAK,CAAA;QAChC,QAAQ,IAAI,EAAE,CAAC;YACb,KAAK,IAAI,CAAC,KAAK,CAAC,CAAC,OAAO,MAAM,CAAC,GAAG,EAAE,GAAG,CAAC,CAAA;YACxC,KAAK,IAAI,CAAC,OAAO,CAAC,CAAC,OAAO,SAAS,CAAC,GAAG,EAAE,GAAG,CAAC,CAAA;YAC7C,KAAK,IAAI,CAAC,gBAAgB,CAAC,CAAC,OAAO,kBAAkB,CAAC,GAAG,EAAE,GAAG,CAAC,CAAA;QACjE,CAAC;IACH,CAAC;AACH,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"random-by-weight-model.js","sourceRoot":"","sources":["../src/random-by-weight-model.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,cAAc,EAAE,MAAM,sBAAsB,CAAA;AAOrD,MAAM,UAAU,mBAAmB,CAAI,KAAsB;IAC3D,MAAM,EAAE,KAAK,EAAE,GAAG,cAAc,CAAC,KAAK,CAAC,CAAA;IACvC,OAAO,KAAK,CAAA;AACd,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"random-index-by-weight.js","sourceRoot":"","sources":["../src/random-index-by-weight.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,kBAAkB,EAAE,MAAM,6BAA6B,CAAA;AAEhE,MAAM,UAAU,mBAAmB,CAAC,OAA8B;IAChE,OAAO,kBAAkB,CACvB,IAAI,CAAC,MAAM,EAAE,EACb,CAAC,EAAE,CAAC,EACJ,OAAO,CACR,CAAA;AACH,CAAC"}
|
|
@@ -1,9 +1,5 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
const map_to_range_1 = require("./map-to-range");
|
|
5
|
-
function randomIntInclusive(min, max) {
|
|
6
|
-
return Math.floor((0, map_to_range_1.mapToRange)(Math.random(), 0, 1, Math.ceil(min), Math.floor(max) + 1));
|
|
1
|
+
import { mapToRange } from './map-to-range.js';
|
|
2
|
+
export function randomIntInclusive(min, max) {
|
|
3
|
+
return Math.floor(mapToRange(Math.random(), 0, 1, Math.ceil(min), Math.floor(max) + 1));
|
|
7
4
|
}
|
|
8
|
-
exports.randomIntInclusive = randomIntInclusive;
|
|
9
5
|
//# 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":"
|
|
1
|
+
{"version":3,"file":"random-int-inclusive.js","sourceRoot":"","sources":["../src/random-int-inclusive.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,mBAAmB,CAAA;AAE9C,MAAM,UAAU,kBAAkB,CAAC,GAAW,EAAE,GAAW;IACzD,OAAO,IAAI,CAAC,KAAK,CAAC,UAAU,CAC1B,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"}
|
package/lib/random-int.js
CHANGED
|
@@ -1,9 +1,5 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
const map_to_range_1 = require("./map-to-range");
|
|
5
|
-
function randomInt(min, max) {
|
|
6
|
-
return Math.floor((0, map_to_range_1.mapToRange)(Math.random(), 0, 1, Math.ceil(min), Math.floor(max)));
|
|
1
|
+
import { mapToRange } from './map-to-range.js';
|
|
2
|
+
export function randomInt(min, max) {
|
|
3
|
+
return Math.floor(mapToRange(Math.random(), 0, 1, Math.ceil(min), Math.floor(max)));
|
|
7
4
|
}
|
|
8
|
-
exports.randomInt = randomInt;
|
|
9
5
|
//# 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":"
|
|
1
|
+
{"version":3,"file":"random-int.js","sourceRoot":"","sources":["../src/random-int.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,mBAAmB,CAAA;AAE9C,MAAM,UAAU,SAAS,CAAC,GAAW,EAAE,GAAW;IAChD,OAAO,IAAI,CAAC,KAAK,CAAC,UAAU,CAC1B,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"}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { randomIndexByWeight } from './random-index-by-weight.js';
|
|
2
|
+
export function randomWeighted(values) {
|
|
3
|
+
const weights = values.map(option => option.weight);
|
|
4
|
+
const index = randomIndexByWeight(weights);
|
|
5
|
+
return values[index];
|
|
6
|
+
}
|
|
7
|
+
//# sourceMappingURL=random-weighted.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"random-weighted.js","sourceRoot":"","sources":["../src/random-weighted.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,mBAAmB,EAAE,MAAM,6BAA6B,CAAA;AAOjE,MAAM,UAAU,cAAc,CAAsB,MAAwB;IAC1E,MAAM,OAAO,GAAG,MAAM,CAAC,GAAG,CAAC,MAAM,CAAC,EAAE,CAAC,MAAM,CAAC,MAAM,CAA0B,CAAA;IAC5E,MAAM,KAAK,GAAG,mBAAmB,CAAC,OAAO,CAAC,CAAA;IAC1C,OAAO,MAAM,CAAC,KAAK,CAAC,CAAA;AACtB,CAAC"}
|
package/lib/random.js
CHANGED
|
@@ -1,9 +1,5 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
const map_to_range_1 = require("./map-to-range");
|
|
5
|
-
function random(min, max) {
|
|
6
|
-
return (0, map_to_range_1.mapToRange)(Math.random(), 0, 1, min, max);
|
|
1
|
+
import { mapToRange } from './map-to-range.js';
|
|
2
|
+
export function random(min, max) {
|
|
3
|
+
return mapToRange(Math.random(), 0, 1, min, max);
|
|
7
4
|
}
|
|
8
|
-
exports.random = random;
|
|
9
5
|
//# sourceMappingURL=random.js.map
|
package/lib/random.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"random.js","sourceRoot":"","sources":["../src/random.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"random.js","sourceRoot":"","sources":["../src/random.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,mBAAmB,CAAA;AAE9C,MAAM,UAAU,MAAM,CAAC,GAAW,EAAE,GAAW;IAC7C,OAAO,UAAU,CACf,IAAI,CAAC,MAAM,EAAE,EACb,CAAC,EAAE,CAAC,EACJ,GAAG,EAAE,GAAG,CACT,CAAA;AACH,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "extra-rand",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.3.0",
|
|
4
4
|
"description": "Yet another random library",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"random"
|
|
@@ -12,43 +12,49 @@
|
|
|
12
12
|
"main": "lib/index.js",
|
|
13
13
|
"types": "lib/index.d.ts",
|
|
14
14
|
"sideEffects": false,
|
|
15
|
+
"type": "module",
|
|
16
|
+
"engines": {
|
|
17
|
+
"node": ">=18.17.0"
|
|
18
|
+
},
|
|
15
19
|
"repository": "git@github.com:BlackGlory/extra-rand.git",
|
|
16
20
|
"author": "BlackGlory <woshenmedoubuzhidao@blackglory.me>",
|
|
17
21
|
"license": "MIT",
|
|
18
22
|
"scripts": {
|
|
19
|
-
"
|
|
20
|
-
"
|
|
21
|
-
"test
|
|
22
|
-
"
|
|
23
|
-
"prepublishOnly": "run-s clean build",
|
|
23
|
+
"prepare": "ts-patch install -s",
|
|
24
|
+
"lint": "eslint --quiet src __tests__",
|
|
25
|
+
"test": "vitest --run",
|
|
26
|
+
"prepublishOnly": "run-s prepare clean build",
|
|
24
27
|
"clean": "rimraf lib",
|
|
25
|
-
"build": "
|
|
26
|
-
"build:compile": "tsc --project tsconfig.build.json --module commonjs --target es2018 --outDir lib",
|
|
27
|
-
"build:patch": "tscpaths -p tsconfig.build.json -s ./src -o ./lib",
|
|
28
|
+
"build": "tsc --project tsconfig.build.json --outDir lib",
|
|
28
29
|
"release": "standard-version"
|
|
29
30
|
},
|
|
30
31
|
"husky": {
|
|
31
32
|
"hooks": {
|
|
32
|
-
"pre-commit": "run-s lint build test",
|
|
33
|
+
"pre-commit": "run-s prepare lint build test",
|
|
33
34
|
"commit-msg": "commitlint -E HUSKY_GIT_PARAMS"
|
|
34
35
|
}
|
|
35
36
|
},
|
|
36
37
|
"devDependencies": {
|
|
37
|
-
"@commitlint/cli": "^
|
|
38
|
-
"@commitlint/config-conventional": "^
|
|
39
|
-
"@
|
|
40
|
-
"
|
|
41
|
-
"@typescript-eslint/parser": "^5.57.0",
|
|
42
|
-
"eslint": "^8.37.0",
|
|
38
|
+
"@commitlint/cli": "^19.2.2",
|
|
39
|
+
"@commitlint/config-conventional": "^19.2.2",
|
|
40
|
+
"@eslint/js": "^9.0.0",
|
|
41
|
+
"eslint": "^9.0.0",
|
|
43
42
|
"husky": "^4.3.8",
|
|
44
|
-
"jest": "^29.5.0",
|
|
45
43
|
"npm-run-all": "^4.1.5",
|
|
46
|
-
"rimraf": "^
|
|
44
|
+
"rimraf": "^5.0.5",
|
|
47
45
|
"standard-version": "^9.5.0",
|
|
48
|
-
"ts-
|
|
49
|
-
"
|
|
50
|
-
"
|
|
51
|
-
"typescript": "^
|
|
46
|
+
"ts-patch": "^3.1.2",
|
|
47
|
+
"tslib": "^2.6.2",
|
|
48
|
+
"typescript": "^5.4.5",
|
|
49
|
+
"typescript-eslint": "^7.7.0",
|
|
50
|
+
"typescript-transform-paths": "^3.4.7",
|
|
51
|
+
"vite": "^5.2.9",
|
|
52
|
+
"vite-tsconfig-paths": "^4.3.2",
|
|
53
|
+
"vitest": "^1.5.0"
|
|
52
54
|
},
|
|
53
|
-
"dependencies": {
|
|
55
|
+
"dependencies": {
|
|
56
|
+
"@blackglory/errors": "^3.0.3",
|
|
57
|
+
"extra-utils": "^5.7.0",
|
|
58
|
+
"justypes": "^4.2.1"
|
|
59
|
+
}
|
|
54
60
|
}
|
package/src/index.ts
CHANGED
|
@@ -1,7 +1,11 @@
|
|
|
1
|
-
export * from './random'
|
|
2
|
-
export * from './random-int'
|
|
3
|
-
export * from './random-int-inclusive'
|
|
4
|
-
export * from './random-
|
|
1
|
+
export * from './random.js'
|
|
2
|
+
export * from './random-int.js'
|
|
3
|
+
export * from './random-int-inclusive.js'
|
|
4
|
+
export * from './random-bool.js'
|
|
5
|
+
export * from './random-index-by-weight.js'
|
|
6
|
+
export * from './random-weighted.js'
|
|
7
|
+
export * from './random-by-weight-model.js'
|
|
8
|
+
export * from './random-by-model.js'
|
|
5
9
|
|
|
6
|
-
export * from './map-to-range'
|
|
7
|
-
export * from './map-to-index-by-weight'
|
|
10
|
+
export * from './map-to-range.js'
|
|
11
|
+
export * from './map-to-index-by-weight.js'
|
|
@@ -1,9 +1,10 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { NonEmptyArray } from 'justypes'
|
|
2
|
+
import { mapToRange } from './map-to-range.js'
|
|
2
3
|
|
|
3
4
|
export function mapToIndexByWeight(
|
|
4
5
|
value: number
|
|
5
6
|
, oldMin: number, oldMax: number
|
|
6
|
-
, weights: number
|
|
7
|
+
, weights: NonEmptyArray<number>
|
|
7
8
|
): number {
|
|
8
9
|
const newMin = 0
|
|
9
10
|
const newMax = weights.reduce((acc, cur) => acc + Math.max(cur, 0))
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import { Getter, NonEmptyArray } from 'justypes'
|
|
2
|
+
import { isArray, isFunction, isNumber } from 'extra-utils'
|
|
3
|
+
import { randomByWeightModel } from './random-by-weight-model.js'
|
|
4
|
+
import { random } from './random.js'
|
|
5
|
+
import { randomInt } from './random-int.js'
|
|
6
|
+
import { randomIntInclusive } from './random-int-inclusive.js'
|
|
7
|
+
|
|
8
|
+
export enum Type {
|
|
9
|
+
Float
|
|
10
|
+
, Integer
|
|
11
|
+
, IntegerInclusive
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export type IRandomModel =
|
|
15
|
+
| number
|
|
16
|
+
| Getter<number>
|
|
17
|
+
| {
|
|
18
|
+
type: Type
|
|
19
|
+
min: number
|
|
20
|
+
max: number
|
|
21
|
+
}
|
|
22
|
+
| NonEmptyArray<{
|
|
23
|
+
weight: number
|
|
24
|
+
value: IRandomModel
|
|
25
|
+
}>
|
|
26
|
+
|
|
27
|
+
export function randomByModel(model: IRandomModel): number {
|
|
28
|
+
if (isNumber(model)) {
|
|
29
|
+
return model
|
|
30
|
+
} else if (isFunction(model)) {
|
|
31
|
+
return model()
|
|
32
|
+
} else if (isArray(model)) {
|
|
33
|
+
const subModel = randomByWeightModel(model)
|
|
34
|
+
return randomByModel(subModel)
|
|
35
|
+
} else {
|
|
36
|
+
const { type, min, max } = model
|
|
37
|
+
switch (type) {
|
|
38
|
+
case Type.Float: return random(min, max)
|
|
39
|
+
case Type.Integer: return randomInt(min, max)
|
|
40
|
+
case Type.IntegerInclusive: return randomIntInclusive(min, max)
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { NonEmptyArray } from 'justypes'
|
|
2
|
+
import { randomWeighted } from './random-weighted.js'
|
|
3
|
+
|
|
4
|
+
export type IWeightModel<T> = NonEmptyArray<{
|
|
5
|
+
weight: number
|
|
6
|
+
value: T
|
|
7
|
+
}>
|
|
8
|
+
|
|
9
|
+
export function randomByWeightModel<T>(model: IWeightModel<T>): T {
|
|
10
|
+
const { value } = randomWeighted(model)
|
|
11
|
+
return value
|
|
12
|
+
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { NonEmptyArray } from 'justypes'
|
|
2
|
+
import { mapToIndexByWeight } from './map-to-index-by-weight.js'
|
|
3
|
+
|
|
4
|
+
export function randomIndexByWeight(weights: NonEmptyArray<number>): number {
|
|
5
|
+
return mapToIndexByWeight(
|
|
6
|
+
Math.random()
|
|
7
|
+
, 0, 1
|
|
8
|
+
, weights
|
|
9
|
+
)
|
|
10
|
+
}
|
package/src/random-int.ts
CHANGED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { randomIndexByWeight } from './random-index-by-weight.js'
|
|
2
|
+
import { NonEmptyArray } from 'justypes'
|
|
3
|
+
|
|
4
|
+
export interface IWeighted {
|
|
5
|
+
weight: number
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
export function randomWeighted<T extends IWeighted>(values: NonEmptyArray<T>): T {
|
|
9
|
+
const weights = values.map(option => option.weight) as NonEmptyArray<number>
|
|
10
|
+
const index = randomIndexByWeight(weights)
|
|
11
|
+
return values[index]
|
|
12
|
+
}
|
package/src/random.ts
CHANGED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export declare function randomByWeight(weights: number[]): number;
|
package/lib/random-by-weight.js
DELETED
|
@@ -1,9 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.randomByWeight = void 0;
|
|
4
|
-
const map_to_index_by_weight_1 = require("./map-to-index-by-weight");
|
|
5
|
-
function randomByWeight(weights) {
|
|
6
|
-
return (0, map_to_index_by_weight_1.mapToIndexByWeight)(Math.random(), 0, 1, weights);
|
|
7
|
-
}
|
|
8
|
-
exports.randomByWeight = randomByWeight;
|
|
9
|
-
//# sourceMappingURL=random-by-weight.js.map
|
|
@@ -1 +0,0 @@
|
|
|
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"}
|