Roblox Forum banner

math.random error.

311 Views 3 Replies 4 Participants Last post by  waguwagu1
Hello. I have a problem. I have to get a part to move to random positions using math. I typed out "local RandomPosition = math.random(1,5)"
and did some code that would check what the variable would be equal to. But it only would equal the first randomposition. Can you figure out how to get this fixed?
1 - 4 of 4 Posts
If I remember correctly you need to change the seed each time by setting the math.randomseed().
Hello. I have a problem. I have to get a part to move to random positions using math. I typed out "local RandomPosition = math.random(1,5)"
and did some code that would check what the variable would be equal to. But it only would equal the first randomposition. Can you figure out how to get this fixed?
You need to run math.random within the scope of where you need the variable to be updated. If you call it at the beginning of the script, the value will always be the same.

Code:
local randomNumber = math.random(1, 5)

for _ = 1, 10 do
    print(randomNumber) --> always the same number
end

for _ = 1, 10 do
    print(math.random(1, 5)) --> randomized output every iteration
end
  • Like
Reactions: 1
Oral calculations can make independent math study more manageable, while also enhancing memory, concentration, and attention. There are various tricks in mathematics that can simplify calculations without the need for a calculator, such as those demonstrated in three-digit addition worksheets over here. By mastering these tricks, students can conduct mental calculations more easily and improve their brainpower.
1 - 4 of 4 Posts
Top