3

I'm working on a web based game where we have multiple minigames. We're insisting on also adding a Crash game, which is pretty popular in the gambling world. However we have been struggling to understand the concept of generating a random (almost unpredictable) number. Most gambling sites provide hashes etc to prove that the numbers are untampered with. We don't really need this because there's no real money involved in our game. So we're only looking for a way to generate a (almost unpredictable) random number (which starts from 1 and goes to infinity) but which gives us a bit of house edge (1/2%).

sneaker
  • 49
  • 2
  • 8

1 Answers1

2

For a (almost unpredictable) random number, you can use Crypto.getRandomValues() which is safer to use and more random than the default Math.random() method.

For a crash game, if you want that the probability of the crash point to be higher than X should be 1/X, you can use this method:

function getCrashPoint() {
    e = 2**32
    h = crypto.getRandomValues(new Uint32Array(1))[0]
    return Math.floor((100*e-h) / (e-h)) / 100
}

If you want to add 2% for the house edge, just add an if statement:

function getCrashPoint() {
    e = 2**32
    h = crypto.getRandomValues(new Uint32Array(1))[0]
    // if h % (100 / desired_precentage) is 0 then the game will crash immediately 
    if (h % 50 == 0) return 1
    return Math.floor((100*e-h) / (e-h)) / 100
}
Mordy Stern
  • 797
  • 6
  • 12