Created
December 27, 2018 17:17
-
-
Save jjhesk/97c21405d302e5bb2773d7954dd33210 to your computer and use it in GitHub Desktop.
explain hash from the given random hash
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 gameResult = (seed) => { | |
const nBits = 52; // number of most significant bits to use | |
// 1. r = 52 most significant bits | |
seed = seed.slice(0, nBits / 4); | |
const r = parseInt(seed, 16); | |
// 2. X = r / 2^52 | |
let X = r / Math.pow(2, nBits); // uniformly distributed in [0; 1) | |
// 3. X = 99 / (1-X) | |
X = 99 / (1 - X); | |
// 4. 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
can you please reverse the calculations?