0

I'm writing Lucky Sevens and I'm getting mostly expected results based on how dice games work IRL. But every once in a while I get a crazy result.

Specifically I am getting a situation where I will go up 100+ dollars, with hundreds of total rolls but it tells me the rolls at my moneyMax is 2 or something impossible like that.

Is this the small probability of random number generators or do I have something wrong?

Here's the Js:

    function rollDice() {
      return Math.floor(Math.random() * 6) + 1;
    }

    function playGame() {
      var startingBet = document.forms["luckySevens"]["startingBet"].value;
      var gameMoney = startingBet;
      var die1;
      var die2;
      var diceSum;
      var rollsNumber = 0;
      var moneyMax = 0;
      var rollsAtMax = 0;

      if (startingBet <= 0) {
      alert("Starting bet must be greater than zero!");
      return false;
      } //end of alert if loop

      while (gameMoney > 0) {
       die1 = rollDice();
       die2 = rollDice();
       diceSum = die1 + die2;
       rollsNumber++;
       if (diceSum != 7) {
        gameMoney = gameMoney - 1;
       } else {
        if (diceSum == 7) {
         gameMoney = gameMoney + 4;
         if (gameMoney > moneyMax) {
           moneyMax = gameMoney;
          rollsAtMax = rollsNumber;
          }
         }
        }
       }
       document.getElementById("results").style.display = "block";
       document.getElementById("playButton").innerText = "Play Again!";
       document.getElementById("betStart").innerText = startingBet;
       document.getElementById("rollsNumber").innerText = rollsNumber;
       document.getElementById("moneyMax").innerText = moneyMax;
       document.getElementById("rollsAtMax").innerText = rollsAtMax;
       return false;
       }//end of playGame()
LukStorms
  • 28,916
  • 5
  • 31
  • 45
Dingle123
  • 11
  • 2

1 Answers1

0

If you doubt the randomness of Math.random()?
An alternative for Math.random has been ask before on SO. For example here

But you could also simply double the randomization to make it even more random.

For example:

function rollDice() {
    return (Math.floor(Math.pow(10,14)*Math.random()*Math.random())%6)+1;
}

function rollDices(rolls){
   let roll = 0;
   let eyes = 0;
   let dice = 0;
   let interval = 0;
   eyesCount = {1:0, 2:0, 3: 0, 4:0, 5:0, 6:0} 
   while (roll < rolls){
    roll++;
    eyes = rollDice();
    eyesCount[eyes]++;
   }
   return {rolls: roll, eyesCount : eyesCount}
  }
  
console.log(rollDices(1000000));

And have a look at the results of this snippet.
It calculates the longest interval that the same eyes are rolled for 2 dices.

function rollDice() {
    return (Math.floor(Math.pow(10,14)*Math.random()*Math.random())%6)+1;
}

function rollOdds(rolls){
   let roll = 0;
   let eyes = 0;
   let dice1 = 0;
   let dice2 = 0;
   let interval = 0;
   eyesCount = {2:0, 3: 0, 4:0, 5:0, 6:0, 7:0, 8:0, 9:0, 10:0, 11:0, 12:0}
   eyesPrevRoll = {2:0, 3: 0, 4:0, 5:0, 6:0, 7:0, 8:0, 9:0, 10:0, 11:0, 12:0}
   eyesMaxInterval = {2:0, 3: 0, 4:0, 5:0, 6:0, 7:0, 8:0, 9:0, 10:0, 11:0, 12:0}
   
   while (roll < rolls){
    roll++;
    dice1 = rollDice();
    dice2 = rollDice();
    eyes = dice1 + dice2;
    eyesCount[eyes]++;
    interval = roll - eyesPrevRoll[eyes];
    if(interval > eyesMaxInterval[eyes]){
     eyesMaxInterval[eyes] = interval;
    }
    eyesPrevRoll[eyes] = roll;
   }
   return {rolls: roll, eyesCount : eyesCount, eyesMaxInterval: eyesMaxInterval}
  }
  
console.log(rollOdds(100000))

It shows that it could happen that a seven isn't rolled within 50+ rolls.

But still, a max amount of 2 with a startbet of 100 would mean that a seven wasn't rolled for 98 times.
So yeah, that makes one wonder about the true randomness of Math.random().

A test snippet based on your code:

function rollDice() {
    return (Math.floor(Math.pow(10,14)*Math.random()*Math.random())%6)+1;
}

function playGame(startingBet) {
      
      let gameMoney = startingBet;
      let die1;
      let die2;
      let diceSum;
      let rollsNumber = 0;
      let moneyMax = 0;
      let rollsAtMax = 0;
      let moneyAt7 = 4;
      let roll = 0;
      let rollLimit = startingBet * 10;

      while (gameMoney > 0 && roll < rollLimit) {
       roll++;
       die1 = rollDice();
       die2 = rollDice();
       diceSum = die1 + die2;
       rollsNumber++;
       if (diceSum !== 7) {
        gameMoney = gameMoney - 1;
       } else {
        if (diceSum === 7) {
         
         gameMoney = gameMoney + moneyAt7;
         
         if (gameMoney > moneyMax) {
           moneyMax = gameMoney;
          rollsAtMax = rollsNumber;
          }
         }
        }
       }
    return { 
      startingBet : startingBet,
      gameMoney : gameMoney,
      moneyMax : moneyMax,
      rollsNumber : rollsNumber,
      rollsAtMax : rollsAtMax
    }; 
  
}

// TEST 
console.log(playGame(10000));

Remark, it's interesting to notice that with a reward of 4 for seven, that the maximum amount is often achieved under 100 rolls.

To be more fair, the win reward for seven should be at least 5 times the loss amount.

Which kinda signifies that one should quit this game when ahead. Because it can only gets worse.

LukStorms
  • 28,916
  • 5
  • 31
  • 45