Math.random property

Hello developers!

  • What dose math.random do?
  • Is it useful?
  • How dose it work?
22 Likes

So math.random is soppost to give you a random number.

4 Likes

Math.random is a math function that makes random numbers for you. This can be helpful on many occasions such as making a minigame, weather or basically anything that needs random numbers.

Also havent you heard of the roblox API? It’s better to find here some questions but you really don’t want to ask here because your amswer is simple. Try checking the Roblox API, just try to post here maybe if you need clarifications.

25 Likes

math.random is used for generating a random number. It’s parameters are 2 numbers.

local randomnumber = math.random(0, 1000)
print(randomnumber)

That script would print a random number from 1 to 1,000.

56 Likes

Thats answer your question? :slight_smile:

That will error. You can’t have infinity as an interval in math.random

image

2 Likes

Sorry, I forgot. I will fix it.

1 Like

Math.random returns a random integer between two numbers.

Dice script

function RollDice()
     return math.random(1,6)
end

print(RollDice())


Output

4
10 Likes

I implore you to search the forum and the API documentation of Roblox on developer.roblox.com before asking questions here on the devforum


math.random is a function, not a property, that returns a random number generated from an algorithm. This function has two optional arguments: a minimum integer value, and a maximum integer value, which returns an integer between the two. Calling math.random with no arguments passed will return a float (decimal) from 0 - 1.

With arguments:

local Rand = math.random(1, 6)
print(Rand) -- a random integer between 0 and 6

Without arguments:

local Rand = math.random()
print(Rand) -- returns a random, decimal number between 0 and 1

It is a useful and commonly used function. The can be used for choosing a random player to assign them a role, choosing a random minigame, etc. The uses of math.random are basically limitless

math.random uses a pseudorandom number generator with a given seed that can be set using math.randomseed. When using this algorithm, you will sometimes get repeated numbers

9 Likes

it returns random number Example:
local function random()
return math.random(1,100)
end
print(random())
Output:
23 – Script:2,function random()

2 Likes

This function returns a random number between two numbers that you indicate, let’s imagine that we have the number 2 and the number 15, if we put a math.random between these two numbers it would return another random number that meets the condition of being between those two numbers:

while wait(1) do
   print(math.random(2,15)
end

This would be putting random numbers between 2 and 15 all the time in the Output, with this exercise it will be better understood, if you only put the first parameter, it will be set as 1, that is, it would be like putting math.random (1, number).

1 Like

it dose be random number make.

1 Like

Let’s just say a random number between 1 and 6 (basically a dice) so let’s say:

Dice = math.random(1, 6)
print("We rolled a "..Dice)
1 Like

Hi Dev.

You can use it for several things, one of them is a check for a number if chosen, which is:

local Numbers = math.random(1,10) -- Numbers

if (Numbers == 5) then
	print("Number 5") -- Print Is Number Chosen
elseif (Numbers == 1) then
	print("Number 1") -- Print Is Number Chosen
end
4 Likes

I personally wouldn’t use math:random(). It’s good for basic game use, but the sequence of numbers is predictable. This is the actual C function that is used:

inline int rand()
{
	return rseed = (rseed * 1103515245 + 12345) & RAND_MAX;
}

This is known as a linear congruential generator. It will basically run through ALL 2^31 - 1 integers before repeating. The sequence is always the same. The only thing that the seed does is change the starting place in the sequence.

A better random number generator that Roblox provides is the Random class. I believe that this one uses a variation of the ARC4 crypto cipher to generate random numbers.

5 Likes

Very useful, thanks. I had a problem with math.random(). It constantly gave me the same number each time it was ran.

Were you using the same seed to initialize it?

math.random is used to generate random numbers between the given parameters and returns the random number.

Full documentation:

1 Like

This probably happened because you set the random number right away in the script. If you put it into a function the number will always change when that function is called. See this example from one of my friends games:

local debounceForClicks = false
local events = game.ReplicatedStorage.Events

events.Click.OnServerEvent:Connect(function(plr)
	local randomNum = math.random(1,5) -- we call this IN THE FUNCTION, not outside.
	print(randomNum) -- prints the number
end)

Everytime you call this function, it will generate a random number. Since I used very few numbers (1, 2, 3, 4, & 5), it mostly called on the same number once or twice but then would switch it up afterwards! :smile:

1 Like

It’s not because you called it right away. When creating variables, a copy of the data is created once. So, if you did this:

local random = math.random(1,6)

It would only run the function once and as a result, would only keep one random number.