Understanding math.random

math.random() selects a random number

by GreatValuePeas

Author Avatar

What is Math.random()

math.random() is a function that will select a random number if given parameters. An example would be...


Notice how I input the numbers 1 and 5 in the parenthesis. These are the

parameters. Because I input those specific numbers, the game will select a

random number between 1 and 5.

If you do not add numerical parameters, the game will choose a number

between 0 and 1, which will get you really wonky decimals. I would rather you

choose numbers, such as 1 and 5.

Examples of Math.random()

local ranNum = math.random(1,10)

In this code, I assign the Math.random() function to a variable, which I higly

recommend you do. The parameters of this function is 1 and 10, so a random

number will be chosen between 1 and 10. I included "Print(ranNum)" so I can

see which number the game has selected

local mysteryN = math.random(1,25)

In this one, I assign the function to a variable like I typically do. This time,

the parameters are 1 and 25, meaning that the game will pick a number between

1 and 25. It will then print my variable, which will show me the random number

it selected.

Okay, so we get that it chooses random numbers.. But HOW is this useful???

Math.random() can be useful if you assign different functions to a number.

For example, if Math.random picks the number 2, we can have a certain action

fired, but if it picks 3, then a different action can be fired.

Here is an example in code form:

local ranNum = math.random(1,2)

if ranNum == 1 then

print("I Chose 1")

if ranNum == 2 then
print("I chose 2")

Lets break this down:

I assigned math.random() to a variable as always, where its gonna pick a number

between 1 and 2.

Then I used an if statement... I will have a tutorial on certain format for if statements

Basically, ranNum will have a random number between 1 and 2 selected. THEN, it

will check if that random number is the number 1. If the number is one, then

it will print (or "say") "I chose 1"

The next if statement will check if the random number selected was 2. If the random

number was 2, then it will print (or "say") "I chose 2"

What are some events I could assign to different numbers?

You could actually make an unboxing system. For example, you could make parameters

of 1,3. If you set numbers 2 and 3 to give you the same weapon, and number 1 to

give you a different weapon, you have an unboxing system. It is important to

use probability, because there is a 66% chance of a player getting numbers 2 and 3, and only a

33% chance of a player getting a 1.

You could also be a little less advanced and just make some simple changes to experiment with yourself.

You could change a Bricks (part) color depending on the number that was chosen.

For example, if the number chosen was 2, the brick would be purple, but if it chose a 4, then the color

can be green.

Honestly, you can make your own events fire from a math.random() script, the more

you dive into experimentation, the more you will understand the topic.

Tips for math.random()

-if you are using math.random(), its best to assign it to a variable

parenthesis (and make sure the smaller number is first, and separated by a comma)

This concludes the lesson on math.random()!

You may use any code I used for examples in your game! Its all up to you!

View in-game to comment, award, and more!