deterministic-dice 1.0.0 → 3.0.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 CHANGED
@@ -1,4 +1,4 @@
1
- # deterministic-dice
1
+ # 🎲 deterministic-dice
2
2
 
3
3
  Deterministic random number generator seeded by a bytes32 hash.
4
4
 
@@ -17,15 +17,15 @@ const dice = new DeterministicDice(
17
17
  "0x1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef"
18
18
  );
19
19
 
20
- dice.roll(15); // 0-15
21
- dice.roll(6); // 0-6
22
- dice.roll(100); // 0-100
20
+ dice.roll(16); // 0-15
21
+ dice.roll(6); // 0-5
22
+ dice.roll(100); // 0-99
23
23
  ```
24
24
 
25
25
  ## How It Works
26
26
 
27
27
  1. Initialize with a bytes32 hash (with or without `0x` prefix)
28
- 2. Call `roll(max)` to get a random number from 0 to max (inclusive)
28
+ 2. Call `roll(n)` to get a random number from 0 to n-1 (n possible values)
29
29
  3. Entropy is automatically rehashed with SHA256 when exhausted
30
30
  4. Same hash always produces the same sequence of rolls
31
31
 
package/dist/index.d.ts CHANGED
@@ -6,9 +6,9 @@ export declare class DeterministicDice {
6
6
  private position;
7
7
  constructor(randomHash: string);
8
8
  /**
9
- * Roll a random number from 0 to max (inclusive).
9
+ * Roll a random number from 0 to n-1 (n possible values).
10
10
  */
11
- roll(max: number): number;
11
+ roll(n: number): number;
12
12
  private consumeHex;
13
13
  }
14
14
  export default DeterministicDice;
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAGA;;GAEG;AACH,qBAAa,iBAAiB;IAC5B,OAAO,CAAC,OAAO,CAAS;IACxB,OAAO,CAAC,QAAQ,CAAS;gBAEb,UAAU,EAAE,MAAM;IAO9B;;OAEG;IACH,IAAI,CAAC,GAAG,EAAE,MAAM,GAAG,MAAM;IAezB,OAAO,CAAC,UAAU;CAYnB;AAED,eAAe,iBAAiB,CAAC"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAGA;;GAEG;AACH,qBAAa,iBAAiB;IAC5B,OAAO,CAAC,OAAO,CAAS;IACxB,OAAO,CAAC,QAAQ,CAAS;gBAEb,UAAU,EAAE,MAAM;IAO9B;;OAEG;IACH,IAAI,CAAC,CAAC,EAAE,MAAM,GAAG,MAAM;IAevB,OAAO,CAAC,UAAU;CAcnB;AAED,eAAe,iBAAiB,CAAC"}
package/dist/index.js CHANGED
@@ -1,5 +1,5 @@
1
- import { sha256 } from "@noble/hashes/sha256";
2
- import { bytesToHex } from "@noble/hashes/utils";
1
+ import { keccak_256 } from "@noble/hashes/sha3";
2
+ import { bytesToHex, hexToBytes } from "@noble/hashes/utils";
3
3
  /**
4
4
  * Deterministic random number generator seeded by a bytes32 hash.
5
5
  */
@@ -11,10 +11,10 @@ export class DeterministicDice {
11
11
  this.position = 0;
12
12
  }
13
13
  /**
14
- * Roll a random number from 0 to max (inclusive).
14
+ * Roll a random number from 0 to n-1 (n possible values).
15
15
  */
16
- roll(max) {
17
- const range = max + 1;
16
+ roll(n) {
17
+ const range = n;
18
18
  const bitsNeeded = Math.ceil(Math.log2(range));
19
19
  const hexCharsNeeded = Math.max(1, Math.ceil(bitsNeeded / 4));
20
20
  const maxValue = Math.pow(16, hexCharsNeeded);
@@ -29,7 +29,9 @@ export class DeterministicDice {
29
29
  let result = 0;
30
30
  for (let i = 0; i < count; i++) {
31
31
  if (this.position >= this.entropy.length) {
32
- this.entropy = bytesToHex(sha256(this.entropy));
32
+ // Convert hex string to bytes, hash the raw bytes (for Solidity parity)
33
+ const bytes = hexToBytes(this.entropy);
34
+ this.entropy = bytesToHex(keccak_256(bytes));
33
35
  this.position = 0;
34
36
  }
35
37
  result = (result << 4) + parseInt(this.entropy[this.position], 16);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "deterministic-dice",
3
- "version": "1.0.0",
3
+ "version": "3.0.0",
4
4
  "description": "Deterministic random number generator seeded by a bytes32 hash",
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",
@@ -37,4 +37,3 @@
37
37
  "typescript": "^5.3.3"
38
38
  }
39
39
  }
40
-