I am currently having an issue with the math.random function in mv, it will only return 0s. I am using Math.floor(Math.random(10)) +1 in an attempt to make something work 1 in 10 times however it will only return a 1, if I remove everything but Math.random it will return 0.xxx but never a number above 0. Am I misunderstanding the use of this function? Is there some way around this or another function I can use to get a random result?
Edit: thank you that worked.
Math.random only returning 0
● ARCHIVED · READ-ONLY
-
-
Math.random returns a number between 0 and 1 (excluding 1).
A common formula is
Math.floor(Math.random() * (maxValue - minValue + 1)) + minValue
eg: if you wanted a value between 5 and 10, then you would multiply (10 - 5 + 1) = 6 by some random number between 0 and 1, then add 5 to it.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/random -
Math.random returns a float between 0.000000 to 0.999999 (etc). Math.floor will just floor that number to a 0 since math.floor rounds the number down to the nearest integer.
To get a range, you have to take
n1 + Math.random() * n2
Where n1 is the starting number and n2 is your end number (non inclusive) minus your starting number.
Then if you want to floor it to the nearest integer you just floor the entire thing
ex. Math.floor(n1 + Math.random() * n2)
edit: I suppose you would call this "ninja'd" xD Hime 1 minute faster than me QAQ -
Math.randomInt(X) is the call to return random integer. (from 0 to X - 1)
Math.randomInt(10) will return a number from 0 to 9
for a range you can use X + Math.randomInt(Y)
5 + Math.randomInt(6) will give you a result from 5 to 10.