Math.random only returning 0

Treosfnb

Warper
Member
Joined
Mar 7, 2016
Messages
4
Reaction score
1
First Language
English
Primarily Uses
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.
 
Last edited by a moderator:

mjshi

Jack of Most Trades
Veteran
Joined
Feb 16, 2013
Messages
969
Reaction score
808
First Language
English
Primarily Uses
N/A
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
 
Last edited by a moderator:

Victor Sant

Veteran
Veteran
Joined
Mar 17, 2012
Messages
1,694
Reaction score
1,452
First Language
Portuguese
Primarily Uses
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.
 
Last edited by a moderator:

Users Who Are Viewing This Thread (Users: 0, Guests: 1)

Latest Threads

Latest Profile Posts

Day 9 of giveaways! 8 prizes today :D
He mad, but he cute :kaopride:

Our latest feature is an interview with... me?!

People4_2 (Capelet off and on) added!

Just beat the last of us 2 last night and starting jedi: fallen order right now, both use unreal engine & when I say i knew 80% of jedi's buttons right away because they were the same buttons as TLOU2 its ridiculous, even the same narrow hallway crawl and barely-made-it jump they do. Unreal Engine is just big budget RPG Maker the way they make games nearly identical at its core lol.

Forum statistics

Threads
106,038
Messages
1,018,466
Members
137,821
Latest member
Capterson
Top