But When this function runs the first loop is actually random.
but then the other 3 times keeps giving repeated
function GetRandom()
for i = 1,3 do
RandomNumber = math.random(1,2)
print("RandNumbOne = ".. tonumber(RandomNumber))
RandomNumber = math.random(1,12)
print("RandNumbTwo = ".. tonumber(RandomNumber))
end
end
GetRandom()
Would you consider adding a break into the for loop?
function GetRandom()
for i = 1,3 do
RandomNumber = math.random(1,2)
print("RandNumbOne = ".. tonumber(RandomNumber))
RandomNumber = math.random(1,12)
print("RandNumbTwo = ".. tonumber(RandomNumber))
break
end
end
GetRandom()
math.randomseed(os.time())
function GetRandom()
for i = 1, 3 do
local RandomNumberOne = math.random(1, 2)
print("RandNumbOne = " .. tostring(RandomNumberOne))
local RandomNumberTwo = math.random(1, 12)
print("RandNumbTwo = " .. tostring(RandomNumberTwo))
end
end
GetRandom()
to make it simple i just added a variable and it prints like this
function GetRandom()
local RandomNumber
for i = 1,3 do
RandomNumber = math.random(1,2)
print("RandNumbOne = ".. tonumber(RandomNumber))
RandomNumber = math.random(1,12)
print("RandNumbTwo = ".. tonumber(RandomNumber))
end
end
GetRandom()
That’s because you’re not localizing your variables, causing the loops that are running before it change the current loops globals, use @jmt99’s example:
Although you don’t really need to use randomseed.
Maybe its cause i over simplified it, its not a exact copy of the code cause i didnt want it to confuse anyone
heres the actual code
function Raid()
--Creating Raiders
for i = 1,3 do
local RandomNumber = math.random(1,2)
local Gender = nil
if RandomNumber == 1 then
Gender = "Male"
else
Gender = "Female"
end
Raider = game.Lighting:FindFirstChild(Gender):Clone()
Raider.Parent = game.Workspace.Raiders
Raider:MoveTo(game.Workspace.SpawnPoint.Position)
Raider.Name = "Raider"
local RandomNumberTwo = math.random(1,12)
local Hair = game.Lighting.Hair:FindFirstChild(Gender.. "-".. tostring(RandomNumberTwo)):Clone()
end
end
Raid()
(Edit: i did put prints inside it and they were just being repeated like the example at the top. so i know its the number generator and not the important lines)
function Raid()
--Creating Raiders
for i = 1,3 do
local RandomNumber = math.random(1,2) print("RandNumbOne = " .. RandomNumber)
local Gender = nil
if RandomNumber == 1 then
Gender = "Male"
else
Gender = "Female"
end
local RandomNumberTwo = math.random(1,12) print("RandNumbTwo = " .. RandomNumberTwo)
--local Hair = game.Lighting.Hair:FindFirstChild(Gender.. "-".. tostring(RandomNumberTwo)):Clone()
end
end
Raid()
Maybe try using a repeat loop instead of a for loop
function Raid()
local tries = 0
--Creating Raiders
repeat
local RandomNumber = math.random(1,2)
local Gender = nil
if RandomNumber == 1 then
Gender = "Male"
else
Gender = "Female"
end
local RandomNumberTwo = math.random(1,12)
local Hair = game.Lighting.Hair:FindFirstChild(Gender.. "-".. tostring(RandomNumberTwo)):Clone()
tries += 1
until tries == 3
end
Raid()
math.random has a tendency to do that. However, you can use Random | Roblox Creator Documentation instead. Random doesn’t have the problem if repeating numbers.
In your code specifically, you would create a Random object using Random.new and call either Random:NextNumber or Random:NextInteger in place of math.random.