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());

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.

but the exercise you mention, the randomly generated event happens within the function:

const getRandEvent = () => {
const random = Math.floor(Math.random() * 3);
if (random === 0) {
return 'Marathon';
} else if (random === 1) {
return 'Triathlon';
} else if (random === 2) {
return 'Pentathlon';
}
};

so each time you call the function, you will get a random event

in your code, you generate the “random” dices once at the start of your program:

let dice1 = Math.floor(Math.random()) * 6 + 1;
let dice2 = Math.floor(Math.random()) * 6 + 1;

then within your function you return the sum of the dice:

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;
};

calling the function multiple times will just use the random values generated at the beginning of your program

what you do, is when you play a dice game, you throw the dice once at the beginning of the game, then each round point to the dice rather then re-rolling them.

that no matter how many times the code is run/the function is called, always yields the same result, might hint that something is wrong with your calculation.

the outcome is always 2 (1 + 1), how could that be?

I flattened the random value before multiplying it by 6

See the difference? The first one is about text editing and a programmer isn’t a typist, a programmer reasons about actions to turn one state of information into another, so if the result is wrong the programmer doesn’t go checking for typos, they’ll go looking for where behaviour went wrong. If you’re looking at the text of it then you’re looking at the wrong thing.