Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

I get the same random number every time, I don't understand?

I'm trying to create new instances of part in a certain area. I'm using a variable to make it easy.

local position1 = math.random (-238.641, -200.141)

local position3 = math.random(-195.442, -154.443)

position 1 is on the x axis, and position 3 is on z axis. every time i create a new part and put,

NewPart1.Position = Vector3.new(position1, 30, position3)

it works! but if i do two, for example,

NewPart2.Position = Vector3.new(position1, 30, position3)

it uses the same exact random number as NewPart1

and when i run the game, they are stacked on top of each other.

Can someone solve my issue?

I've tried making other variables for NewPart2 like

local positionx = math.random (-238.641, -200.141)

local positionz = math.random(-195.442, -154.443)

and it works!, but it gets messy. and i'm trying to put 10 of those. so i'll have to do 8 more different variables. and it's really messy!

4 comments

0

Its because the math.random is inside a variable which doesnt change if you just use it again you have to generate another one to change the output

0

so I would have to make several math.randoms?

0

No use math.randomseed(os.time()) at the top to randomize the seed

0

but i want it to choose a random number in between -238.641 and -200.141 or -195.442 and -154.443

1 answer

✓ Accepted answer

The reason it gets the same position is because math.random is global and returns the same value each time unless you randomize the seed using math.randomseed, computers use a pseudonumber generator which use a seed, and if you don't randomize the seed using math.randomseed, it will generate the same seed and hence, the same numbers. It will always generate the same number, You can either use Random.new() or use math.randomseed with math.random, You also have to use two math.random for the position because you store the number inside a variable

math.randomseed(os.time()) -- If you don't randomize this, the numbers below using math.random generate the same values, for example, if position1 is -230 in one server. The other server also has position1 -230
local position1 = math.random (-238.641, -200.141)

local position3 = math.random(-195.442, -154.443)

NewPart1.Position = Vector3.new(position1, 30, position3)

local part2Position1 = math.random(-238.641, -200.141)
local part2Position3 = math.random(-195.442, -154.443)

NewPart2.Position = Vector3.new(part2Position1, 30, part2Position3)

1 comment

0

yeah i know, i was just making sure there wasn't any easier way to do it. thanks anyways!

This discussion is preserved as it was. New answers, comments, and votes are closed.