Why does dice1 only return 1 even though the it's supposed to be random?

The code below works fine but even though I tried to run it many times, the results of dice1 and total do not change. What’s wrong?

let dice1 = Math.floor(Math.random()) * 6 + 1;
let dice2 = Math.floor(Math.random()) * 6 + 1;
const rollTheDice = () => {
  // Math.random() gives us a random number from 0 up to, but not including, 1
  // We multiplied that by 6 to get a number between 0 and up to, but not including, 6
  // But since we actually wanted numbers from 1 to 6, inclusive, we added 1
    
    return dice1 + dice2;
};
console.log(dice1);
console.log(rollTheDice());

the numbers are generated once at the beginning of the script, not each time the function is called

1 Like

so how do you generate random results?

You’ll need to consider which code is doing the generating, and which part of your code is currently running when you want to be doing that generating. Move the code that generates the random numbers to the location that runs when you want to be doing that.

1 Like