1
var luckyNumbers1 = [1, 2, 3, 4, 5, 6, 7];

var luckyNumber1 = luckyNumbers1[Math.floor(Math.random () * 7)];

console.log ("Your winning numbers are " + luckyNumber1 + ", " +
luckyNumber1 + ", " + luckyNumber1 + "!");

Why do I get the same set of numbers? I noticed that if I do more luckyNumbers the sets will be different. But how come Javascript uses only 1 method to randomize the number of this variable? e.g. :

" Your winning numbers are 1, 1, 1!"
" Your winning numbers are 5, 5, 5!"

thanks, Happy coding!

User863
  • 19,346
  • 2
  • 17
  • 41
Yama
  • 13
  • 3

5 Answers5

3

Because once the variable luckyNumber1 is defined, it does not change afterwards no matter how many times it's referenced unless it gets overwritten (you never overwrite it in your code).

1

Use a function.

var luckyNumbers1 = [1, 2, 3, 4, 5, 6, 7];

function luckyNumber1() {
  return luckyNumbers1[Math.floor(Math.random () * 7)]
  }

console.log ("Your winning numbers are " + luckyNumber1() + ", " +
luckyNumber1() + ", " + luckyNumber1() + "!");
djcaesar9114
  • 1,880
  • 1
  • 21
  • 41
1

var luckyNumbers1 = [1, 2, 3, 4, 5, 6, 7];

var getLuckyNumber1 = () => luckyNumbers1[Math.floor(Math.random() * 7)];

console.log ("Your winning numbers are " + getLuckyNumber1() + ", " +
getLuckyNumber1() + ", " + getLuckyNumber1() + "!");

Because you are using the same variable, you are assigning a value to luckyNumber1 and you just keep reusing that without regenerating the random number. Having it in a function it will do it.

Nicolae Maties
  • 2,476
  • 1
  • 16
  • 26
0

That's the expected behavior for your code. You must understand that the value that you are assigning to luckyNumber1 is evaluated just once, so the Math.random() part of your code run once.

You should put your code inside a function to make it be called multiple times. Also you should use const and let instead of var (see Const in JavaScript: when to use it and is it necessary?).

The correct code should look like this:

const luckyNumbers = [1, 2, 3, 4, 5, 6, 7];

const drawLuckyNumber = () => {
    return luckyNumbers[Math.floor(Math.random () * 7)];
}

console.log ("Your winning numbers are " + drawLuckyNumber() + ", " +
drawLuckyNumber() + ", " + drawLuckyNumber() + "!");
Elias Soares
  • 9,884
  • 4
  • 29
  • 59
  • using the const keyword will let you only use 1 option to log in the console. Because it will remain constant the same. – Yama Jun 03 '20 at 13:38
  • That's not true. Run the snippet. The const here is used a) for the available numbers b) to store the function instead of putting it on global scope. – Elias Soares Jun 03 '20 at 13:46
0

Everyone's kinda tossing out different versions of the same answer. You need to be generating a new value each time instead of generating a value once and referencing it over and over. I'd do this with rando.js to make the randomness simpler and more readable, but that's just a preference. All of the answers here work.

var luckyNumbers1 = [1, 2, 3, 4, 5, 6, 7];
console.log ("Your winning numbers are " + rando(luckyNumbers1).value + ", " + rando(luckyNumbers1).value + ", " + rando(luckyNumbers1).value + "!");
<script src="https://randojs.com/1.0.0.js"></script>

If you want to use this code, don't forget to add the paste the script tag in the head of your html document.

Aaron Plocharczyk
  • 2,776
  • 2
  • 7
  • 15