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
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.
Try this:
setInterval(function(){
number = Math.floor((Math.random()*100)+1);
//other code
}, 1000);
Math.random()*100)+1
calculate a number between o and 100, if you want a different range change the number 100 with 10 for example and you can have a range between 0 to 10
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With