Created
October 2, 2018 05:30
-
-
Save dsetzer/be250ed1df644291ab32e7912e49bc00 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const crypto = require("crypto") | |
function gameResult(seed, salt) { | |
const nBits = 52 // number of most significant bits to use | |
// 1. HMAC_SHA256(key=salt, message=seed) | |
const hmac = crypto.createHmac("sha256", salt) | |
hmac.update(seed) | |
seed = hmac.digest("hex") | |
// 2. r = 52 most significant bits | |
seed = seed.slice(0, nBits/4) | |
const r = parseInt(seed, 16) | |
// 3. X = r / 2^52 | |
let X = r / Math.pow(2, nBits) // uniformly distributed in [0; 1) | |
// 4. X = 99 / (1-X) | |
X = 99 / (1 - X) | |
// 5. return max(trunc(X), 100) | |
const result = Math.floor(X) | |
return Math.max(1, result / 100) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment