@esportsplus/random 0.0.4 → 0.0.6

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/build/item.js CHANGED
@@ -1,4 +1,5 @@
1
1
  import rng from "./rng";
2
+ const BASIS_POINTS = 10000;
2
3
  export default (items, weights) => {
3
4
  if (weights === undefined) {
4
5
  return items[Math.floor(rng() * items.length)];
@@ -6,9 +7,9 @@ export default (items, weights) => {
6
7
  if (items.length !== weights.length) {
7
8
  throw new Error('Random: each item requires a weight');
8
9
  }
9
- let random = rng(), total = 10000;
10
+ let random = rng() * BASIS_POINTS, total = BASIS_POINTS;
10
11
  for (let i = 0, n = weights.length; i < n; i++) {
11
- total += weights[i];
12
+ total -= weights[i];
12
13
  }
13
14
  if (total !== 0) {
14
15
  throw new Error('Random: weights use basis point math, they must add up to 10000 ( 100% )');
package/package.json CHANGED
@@ -14,5 +14,5 @@
14
14
  "prepublishOnly": "npm run build"
15
15
  },
16
16
  "types": "build/index.d.ts",
17
- "version": "0.0.4"
17
+ "version": "0.0.6"
18
18
  }
package/src/item.ts CHANGED
@@ -1,6 +1,9 @@
1
1
  import rng from "./rng";
2
2
 
3
3
 
4
+ const BASIS_POINTS = 10000;
5
+
6
+
4
7
  export default <T>(items: T[], weights?: number[]): T => {
5
8
  if (weights === undefined) {
6
9
  return items[ Math.floor(rng() * items.length) ];
@@ -10,11 +13,11 @@ export default <T>(items: T[], weights?: number[]): T => {
10
13
  throw new Error('Random: each item requires a weight');
11
14
  }
12
15
 
13
- let random = rng(),
14
- total = 10000;
16
+ let random = rng() * BASIS_POINTS,
17
+ total = BASIS_POINTS;
15
18
 
16
19
  for (let i = 0, n = weights.length; i < n; i++) {
17
- total += weights[i];
20
+ total -= weights[i];
18
21
  }
19
22
 
20
23
  if (total !== 0) {