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

I still don’t get it.
In this exercise “https://www.codecademy.com/paths/web-development/tracks/web-dev-js-arrays-loops-objects/modules/learn-javascript-scope/projects/training-days

const event and event2 yield different results, and also every time I run the code.

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.

Thanks for your detailed explanation. But
I tried moving the randomly generated dice into the function. Called the functions twice to test but it doesn’t yield different results.
https://www.codecademy.com/paths/web-development/tracks/getting-started-with-javascript/modules/code-challenge-javascript-fundamentals/lessons/javascript-fundamentals-code-challenge/exercises/fix-the-broken-code

the exercise url will just load my own code (checks for logged in user), please copy paste your code to the forum

Sorry about that!


const rollTheDice = () => {
  const dice1 = Math.floor(Math.random()) * 6 + 1 ;
  const dice2 = Math.floor(Math.random()) * 6 + 1;
  return dice1 + dice2;
};

console.log(rollTheDice());
console.log(rollTheDice());

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 checked the solution code. I put the parentheses in the wrong place ! haha… Thank you!

Such a shame, you where so close. Shame the learning to debug opportunity has been wasted.

1 Like

A better perspective would 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.

1 Like

Of course after comparing my code to the solution I understood what went wrong, not just the typo…

Right, but you found them in backwards order. It would be finding the behaviour, then the typo

And you did, because how would you know to look for problems if the result wasn’t wrong? But you’d want to keep looking at what happened to get that

1 Like

Thanks. I’ll remember that.

You can program like that too, especially while learning (I still usually do)

Write some code and print out what you’ve got after that. Then you can consider what to do next… print out again and so on