deterministic-dice 1.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 +34 -0
- package/dist/index.d.ts +15 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +41 -0
- package/package.json +40 -0
package/README.md
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
# deterministic-dice
|
|
2
|
+
|
|
3
|
+
Deterministic random number generator seeded by a bytes32 hash.
|
|
4
|
+
|
|
5
|
+
## Install
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm install deterministic-dice
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Usage
|
|
12
|
+
|
|
13
|
+
```typescript
|
|
14
|
+
import { DeterministicDice } from "deterministic-dice";
|
|
15
|
+
|
|
16
|
+
const dice = new DeterministicDice(
|
|
17
|
+
"0x1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef"
|
|
18
|
+
);
|
|
19
|
+
|
|
20
|
+
dice.roll(15); // 0-15
|
|
21
|
+
dice.roll(6); // 0-6
|
|
22
|
+
dice.roll(100); // 0-100
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
## How It Works
|
|
26
|
+
|
|
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)
|
|
29
|
+
3. Entropy is automatically rehashed with SHA256 when exhausted
|
|
30
|
+
4. Same hash always produces the same sequence of rolls
|
|
31
|
+
|
|
32
|
+
## License
|
|
33
|
+
|
|
34
|
+
MIT
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Deterministic random number generator seeded by a bytes32 hash.
|
|
3
|
+
*/
|
|
4
|
+
export declare class DeterministicDice {
|
|
5
|
+
private entropy;
|
|
6
|
+
private position;
|
|
7
|
+
constructor(randomHash: string);
|
|
8
|
+
/**
|
|
9
|
+
* Roll a random number from 0 to max (inclusive).
|
|
10
|
+
*/
|
|
11
|
+
roll(max: number): number;
|
|
12
|
+
private consumeHex;
|
|
13
|
+
}
|
|
14
|
+
export default DeterministicDice;
|
|
15
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +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"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import { sha256 } from "@noble/hashes/sha256";
|
|
2
|
+
import { bytesToHex } from "@noble/hashes/utils";
|
|
3
|
+
/**
|
|
4
|
+
* Deterministic random number generator seeded by a bytes32 hash.
|
|
5
|
+
*/
|
|
6
|
+
export class DeterministicDice {
|
|
7
|
+
constructor(randomHash) {
|
|
8
|
+
this.entropy = randomHash.startsWith("0x")
|
|
9
|
+
? randomHash.slice(2)
|
|
10
|
+
: randomHash;
|
|
11
|
+
this.position = 0;
|
|
12
|
+
}
|
|
13
|
+
/**
|
|
14
|
+
* Roll a random number from 0 to max (inclusive).
|
|
15
|
+
*/
|
|
16
|
+
roll(max) {
|
|
17
|
+
const range = max + 1;
|
|
18
|
+
const bitsNeeded = Math.ceil(Math.log2(range));
|
|
19
|
+
const hexCharsNeeded = Math.max(1, Math.ceil(bitsNeeded / 4));
|
|
20
|
+
const maxValue = Math.pow(16, hexCharsNeeded);
|
|
21
|
+
const threshold = maxValue - (maxValue % range);
|
|
22
|
+
let value;
|
|
23
|
+
do {
|
|
24
|
+
value = this.consumeHex(hexCharsNeeded);
|
|
25
|
+
} while (value >= threshold);
|
|
26
|
+
return value % range;
|
|
27
|
+
}
|
|
28
|
+
consumeHex(count) {
|
|
29
|
+
let result = 0;
|
|
30
|
+
for (let i = 0; i < count; i++) {
|
|
31
|
+
if (this.position >= this.entropy.length) {
|
|
32
|
+
this.entropy = bytesToHex(sha256(this.entropy));
|
|
33
|
+
this.position = 0;
|
|
34
|
+
}
|
|
35
|
+
result = (result << 4) + parseInt(this.entropy[this.position], 16);
|
|
36
|
+
this.position++;
|
|
37
|
+
}
|
|
38
|
+
return result;
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
export default DeterministicDice;
|
package/package.json
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "deterministic-dice",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "Deterministic random number generator seeded by a bytes32 hash",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "./dist/index.js",
|
|
7
|
+
"types": "./dist/index.d.ts",
|
|
8
|
+
"exports": {
|
|
9
|
+
".": {
|
|
10
|
+
"types": "./dist/index.d.ts",
|
|
11
|
+
"import": "./dist/index.js"
|
|
12
|
+
}
|
|
13
|
+
},
|
|
14
|
+
"files": [
|
|
15
|
+
"dist"
|
|
16
|
+
],
|
|
17
|
+
"scripts": {
|
|
18
|
+
"build": "tsc",
|
|
19
|
+
"prepublishOnly": "npm run build"
|
|
20
|
+
},
|
|
21
|
+
"keywords": [
|
|
22
|
+
"deterministic",
|
|
23
|
+
"random",
|
|
24
|
+
"dice",
|
|
25
|
+
"prng",
|
|
26
|
+
"ethereum",
|
|
27
|
+
"blockchain",
|
|
28
|
+
"hash",
|
|
29
|
+
"seed"
|
|
30
|
+
],
|
|
31
|
+
"author": "",
|
|
32
|
+
"license": "MIT",
|
|
33
|
+
"dependencies": {
|
|
34
|
+
"@noble/hashes": "^1.3.3"
|
|
35
|
+
},
|
|
36
|
+
"devDependencies": {
|
|
37
|
+
"typescript": "^5.3.3"
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
|