Program1031.comTM
Game Programming Help for Beginners!
Home Questions and Answers Physical and Digital Books

Question:
How do I generate a random number within a range in Game Maker?


Answer:


The code above will create a whole random number between 0 and 9 for you. Why is that?

floor() is a function that rounds down what ever is the argument of it. random() creates a random number. random() uses the argument you put in and will never go over that number when generating a number.

So if you put in 10 as the argument for random, the random() function could generate numbers like 8.99, 9.4532, 9.99, but never 10. So when the floor() rounds the number down, the highest it can be is 9! Isn't that neat!

So if you wanted a number between 0 and 456, how would you do it? Wouldn't you put in this code?

floor(random(457));

Yep! We put in 457 as the argument because the random number will never go over 457, but will give you numbers that will always round down, with the highest being 456!

Try it out!

Page updated: March 23rd, 2015 @ 6:21 AM Eastern Time

Game Maker: Studio
Help, References, and Tutorials
RPG Maker Fes
Help, References, and Tutorials
smileBASIC
Help, References, and Tutorials

Physical and Digital Books
Search Physical and Digital Books at Program1031.com

Home Questions and Answers Email Newsletter
Watch our YouTube channel
Content and Design © 2012-2025 Program1031.com. All Rights Reserved. | Privacy Policy | Terms of Service
All Copyrights and Trademarks of mentioned products are copyrights and trademarks of their respective owners.

As an Amazon Associate and ChristianBook.com Affiliate, Breakthrough Gaming LLC earns a commission on qualifying purchases from Amazon.com and ChristianBook.com through our website. Thank you for your purchases!