extra-rand 0.1.4 → 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
@@ -38,7 +38,7 @@ function randomByWeight(weights: number[]): number
38
38
  The function returns an index of one of weights.
39
39
 
40
40
  ### Map
41
- These low-level methods help you to use random number generators other than `Math.random()`.
41
+ These low-level functions help you to use random number generators other than `Math.random()`.
42
42
 
43
43
  #### mapToRange
44
44
  ```ts
package/package.json CHANGED
@@ -1,12 +1,13 @@
1
1
  {
2
2
  "name": "extra-rand",
3
- "version": "0.1.4",
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
+ }