javascript - MDN example (of Math.random()): could it be parseInt() instead of Math.floor()? -
i reading javascript tutorial , searching functions on mdn website when stumbled across example of math.random()
:
function getrandomintinclusive(min, max) { return math.floor(math.random() * (max - min + 1)) + min; }
while understand math.floor
chooses biggest number , erases numbers have decimal values, learnt function called parseint()
deletes of numbers after point. so, what's difference between two? couldn't use
function getrandominclusive(min, max) { return parseint(math.random() * (max - min + 1)) + min; }
instead? got idea while writing question math.floor()
might write 5 when it's 4,5 , parseint()
write 4, it's not important random number generator understand (if know examples of when important, please tell me!) so, there's still not of difference in case?
parseint
parses string integer, reading digits @ beginning of string. not appropriate use round number integer. number converted string first, unexpected results:
var num = 1000000000000000000000; parseint(num) // 1, because string representation 1e+21
i got idea while writing question math.floor() might write 5 when it's 4,5 , parseint() write 4
you’re thinking of rounding nearest whole number, math.round
does. math.floor
rounds down.
Comments
Post a Comment