Monday, May 13, 2024
 Popular · Latest · Hot · Upcoming
50
rated 0 times [  56] [ 6]  / answers: 1 / hits: 16009  / 11 Years ago, thu, august 22, 2013, 12:00:00

How can you generate a new random number every given second using Math.random()? I have tried putting it in a function and return Math.random but it return the same thing every time. Is there an efficient way to do this in short amount of code?
-thanks


More From » math

 Answers
9
 setInterval(function(){   
console.log(Math.floor((Math.random()*100)+1));

}, 1000);


I ran it in Firefox and it works great.



I will follow TryHunter and edit that the *100 makes it return 1 to 100, and if you want to say 1 to 1000 change it to 1000.


[#76215] Wednesday, August 21, 2013, 11 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
marib

Total Points: 596
Total Questions: 120
Total Answers: 95

Location: Nauru
Member since Thu, Feb 2, 2023
1 Year ago
;