Random | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
20th Jun 2020, 3:58 PM
Kashyap Kumar
Kashyap Kumar - avatar
4 Answers
+ 4
Thanks AJ Anant and Sivaprasad and Seniru
20th Jun 2020, 4:32 PM
Kashyap Kumar
Kashyap Kumar - avatar
+ 2
What you do is storing a random value in a variable and printing it twice, which will produce the same output. What you can do is calling the Math.random twice in appropriate places
20th Jun 2020, 4:01 PM
Seniru
Seniru - avatar
+ 2
Output is same because you are calling random function once and assigning in a single variable. To get different value call random function twice if you want different values then do like this let arr = [1,2,3,4,5]; console.log(Math.floor(Math.random() * arr.length));//first number console.log(Math.floor(Math.random() * arr.length));//second number OR let arr = [1,2,3,4,5]; for(var i = 0; i < 2; i++) { let random = Math.floor(Math.random() * arr.length); console.log(random);//first number } OR as Sivaprasad Did
20th Jun 2020, 4:02 PM
A͢J
A͢J - avatar
+ 1
https://code.sololearn.com/W5cHEZZfqBpJ/?ref=app Output is same because you are storing random number is a single variable. different values please link this
20th Jun 2020, 4:06 PM
Sivaprasad