+ 4

NodeJS Random Number

How can I generate a random number in JavaScript/NodeJS ?

7th Apr 2017, 2:20 AM
MrCoder
MrCoder - avatar
8 Respostas
+ 7
if you want a number in a range use this function: function randVal (min,max){ return Math.floor(Math.random()*(max-min+1)+min); } [edit] and for float values: function randFloat(min, max) { return Math.random() * (max - min) + min; }
7th Apr 2017, 10:16 AM
Burey
Burey - avatar
+ 7
Math.random() and then return random which is between 0 and 1...... Hey!! I know that you know it!
7th Apr 2017, 2:23 AM
Yanothai Chaitawat
Yanothai Chaitawat - avatar
+ 7
no problem :)
7th Apr 2017, 10:30 AM
Burey
Burey - avatar
+ 6
😐 Ummm ok
7th Apr 2017, 2:26 AM
Yanothai Chaitawat
Yanothai Chaitawat - avatar
+ 6
@JRE Thanks bro :D @Burey Thanks to you too man :D
7th Apr 2017, 10:19 AM
MrCoder
MrCoder - avatar
+ 5
@Very Hard(java & c++ & c# & py & (All is hard)) Hey you know I dont😂😂
7th Apr 2017, 2:24 AM
MrCoder
MrCoder - avatar
+ 4
Haha lol😂
7th Apr 2017, 2:27 AM
MrCoder
MrCoder - avatar
+ 3
@MrCoder Its like he said but to get a round number you have to multiply like: Math.floor(Math.random()*100); Its exactly the same on Node.
7th Apr 2017, 7:59 AM
JRE
JRE - avatar