Each result is generated using the following parameters:

  1. client-seed - A randomly generated selection of text that can be modified. This is generated client/browser side.
  2. server-seed - A randomly generated 64 character hex string. You will get a hashed version of this before you start gambling.
  3. Nonce - a sequential bet number

Generating a random number

First we need to generate the client and server seed

function randomString(length)
{
  const availableChars =
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789;
  let randomString = '';
  for(let i = 0; i < length; i++) {
    randomString += availableChars[Math.floor(Math.random() * availableChars.length)];
  }
  return randomString;
}

We can generate random strings with

randomString(30) //
jd2X84d1jvUvIqcaRFmVWIsot2zIeH
randomString(30) // 
XaX6MWShOwxAZbggz4dfTYVd6EuIzw
randomString(30) // 
95zrd3MsUZJ6AOE9OdPu9ctKxmgQpy

For the server seed we are assuming that this will be generated server-side so we use a crypto module for that

/*
generate 256 random characters
*/
const crypto = require('crypto' built-in );
crypto.randomBytes(256).toString('hex');

Putting everything together:

combination = Server Seed + Cliemt Seed + Nonce

Then, we calculate a SHA-512 hash of the combination, that will return us a 128 character hex string:

const crypto = require('crypto');
const hash = crypto.createHash('sha512').update(combination).digest('hex');
console.log(hash) // XaX6MWShOwxAZbggz4dfTYVd6EuIzw...

GOOD now that we have the combination( server-seed + client seed + nonce) and function to perform sha512 We can provide the client with the result by converting the hashed combination into a number. We do this in the following way:

const getResult = hashedValue => {
// the offset of the interval
let index = 0;
// result variable
let result;

do {
   // get the decimal value from an interval of 5 hex letters
   result = parseInt(hashedValue.subString(index * 5, index * 5 + 5), 16);
   // increment the offset incase we will repeat the operation above
   index += 1;
   // if all numbers we over 999999 amd we reach the end of the string, set to a default value of 9999(99 as result)
   if (index * 5 + 5 > 129) {
       result = 9999;
       break;
      }
    } while (result >= 1e6);
   // the result is between 0-999999 and we need to convert to 4 digit number
   // we apply a modulas and a 4 digit numbet is further split into a 2 digit number 
      return [result % 1e4] * 1e2;
    };
console.log(getResult('d9b9b9774d35566bdbd33a0050b15f7f3bc0ec68cff1b593994c04b05767573c0c002cb965d506e5ccedf803db8b58c27406e320f2c13dcbc247448dedccec12'))

We have now the knowledge to implement a full provably fair to be useful and functional we will need to take care of implementing the client-server communication, which means we’ll need a server to handle the server-side operations like generating a server-seed or calculating the roll result and returning that to the client with api. I'm not going to get into exact steps in creating the server for it. You just need to implement the http server and adding routes and save the users and rolls into a database.