0

Random number picker _ help

Run this code. Just for test, in the first input type 4, in the next one type 5. Then click pick. No matter how many times you click on it you’ll always get 4. In short_ it will never pick the max value. For example 1 - 100: it will never pick 100. Why is that, and is there a fix? this code https://code.sololearn.com/WyvJUoo30cRT/?ref=app

4th Jan 2020, 9:58 AM
Ginfio
Ginfio - avatar
2 odpowiedzi
+ 4
Ginfio Captain obvious 🤠 "" The Math.random() function returns a floating-point, pseudo-random number in the range 0–1 (inclusive of 0, but not 1) "" source : https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/random Important thing to note is that it never returns 1 so if you do this : Math.random()*10; it'll never result into 10 but always less than 10 because it multiplies 10 by number less than 1. as you are applying Math.floor() to result of expression it never reaches the upper value (5 for given case) eg: if Math.random() returns 0.46 then Math.random()*10 will be 4.6 if you floor it you'll always get 4 ●Try this quick fix: use Math.round instead of Math.floor
4th Jan 2020, 10:11 AM
🇮🇳Omkar🕉
🇮🇳Omkar🕉 - avatar
+ 3
🇮🇳Omkar🕉 That makes sense a lot. Thanks a lot!😀👍
4th Jan 2020, 10:14 AM
Ginfio
Ginfio - avatar