Skip to content

Instantly share code, notes, and snippets.

@jjhesk
Created December 27, 2018 17:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jjhesk/97c21405d302e5bb2773d7954dd33210 to your computer and use it in GitHub Desktop.
Save jjhesk/97c21405d302e5bb2773d7954dd33210 to your computer and use it in GitHub Desktop.
explain hash from the given random hash
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);
};
@jjhesk
Copy link
Author

jjhesk commented Dec 29, 2018

can you please reverse the calculations?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment