Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

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

Asked by 3 years ago

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!

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 acediamondn123 147 — 3y
0
so I would have to make several math.randoms? superbubba2020 6 — 3y
0
No use math.randomseed(os.time()) at the top to randomize the seed AnasBahauddin1978 715 — 3y
0
but i want it to choose a random number in between -238.641 and -200.141 or -195.442 and -154.443 superbubba2020 6 — 3y

1 answer

Log in to vote
0
Answered by 3 years ago
Edited 3 years ago

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)
0
yeah i know, i was just making sure there wasn't any easier way to do it. thanks anyways! superbubba2020 6 — 3y
Ad

Answer this question