Is there a best way to round a random number between 0-100? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Is there a best way to round a random number between 0-100?

Math.round(Math.random()*100)

20th Feb 2021, 4:54 AM
Jesús Oviedo
Jesús Oviedo - avatar
4 Answers
+ 5
Math.random()*100 //possible highest output 99.99 after floor 99 Math.random()*4 //possible highest output 3.99 after floor 3 Math.random()*n produces highest number is (n-1). So Math.random()*101 will output highest 100.999 after floor will be 100
20th Feb 2021, 5:11 AM
TOLUENE
TOLUENE - avatar
+ 2
Math.floor(Math.random()*101)
20th Feb 2021, 5:06 AM
TOLUENE
TOLUENE - avatar
0
Thanks Sayed. floor returns the largest integer less than or equal to a given number. I want to round it. I'm curious why do you use 101?
20th Feb 2021, 5:06 AM
Jesús Oviedo
Jesús Oviedo - avatar
0
Great. Thanks
20th Feb 2021, 5:12 AM
Jesús Oviedo
Jesús Oviedo - avatar