Math.random only returning 0

● ARCHIVED · READ-ONLY
Started by Treosfnb 4 posts View original ↗
  1. 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.
  2. 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
  3. 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.