#coinflip game only generating 1

10 messages · Page 1 of 1 (latest)

white gazelle

title

Code:

        function calculateResult(gameId, serverSeed, publicSeed) {
            const combinedSeed = gameId + serverSeed;
            const hash = crypto.createHash('sha256').update(combinedSeed).digest('hex');
            const result = parseInt(hash.slice(0, 16), 16); // Use the first 16 characters of the hash
            const normalizedResult = (result % 2) + 1; // Normalize result to 1 or 2
            console.log(normalizedResult);
            return normalizedResult;
        }```
peak ingotBOT
  • Consider reading #how-to-get-help to improve your question!
  • Explain what exactly your issue is.
  • Post the full error stack trace, not just the top part!
  • Show your code!
  • Issue solved? Press the button!
sonic hinge
white gazelle title Code: ``` // Function to calculate the provably fair result ...

This should work™️

function calculateResult(gameId, serverSeed, publicSeed) {
  const combinedSeed = gameId + serverSeed + publicSeed;
  const hash = crypto.createHash('sha256').update(combinedSeed).digest('hex');
  const slice = hash.slice(0, 8);
  const resultValue = parseInt(slice, 16);
  const normalizedResult = (resultValue % 2) + 1;
  console.log(normalizedResult);
  return normalizedResult;
}

Here's how i tested it:

for (let i = 0; i < 10; i++) {
  const gameId = `game${i}`;
  const serverSeed = `server${Math.random()}`;
  const publicSeed = `public${Math.random()}`;
  calculateResult(gameId, serverSeed, publicSeed);
}
hollow heart

That doesn't generate a fair result because of the % operator and is overcomplicated. Use crypto.randomInt().

steady fractal

does flipping a coin need to by cryptographically secure?

fallen matrix

Only if you made a bot that wouldn’t be in line with app directory terms and several local laws in countries discord is reachable from

hollow heart
fallen matrix

So: no; or do you also take your canned food to the physics lab to open it with a laser cutter so the lid is cut with a perfect line instead of using a can opener. Just because it‘s possible to do something more complicated and slightly more accurate doesn’t mean it has any use for a mundane task

neat night

8 lines for a "1 or 2"... i got brain damage