+ 4

# NodeJS Random Number

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

8 Answers

+ 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;
}

+ 7

Math.random() and then return random which is between 0 and 1......
Hey!! I know that you know it!

+ 7

no problem :)

+ 6

đ Ummm ok

+ 6

@JRE Thanks bro :D
@Burey Thanks to you too man :D

+ 5

@Very Hard(java & c++ & c# & py & (All is hard))
Hey you know I dontđđ

+ 4

Haha lolđ

+ 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.