Problem with math.random()

Hi I have some deers spread all around the valley, that are using pathifing service to move around. Here is a cut of my script that is responsible for its moving:

   way = torso.Position
   local path = service:CreatePath()
   local vX = math.random(torso.CFrame.X - 400, torso.CFrame.X + 400)
   local vZ =  math.random(torso.CFrame.Z - 400, torso.CFrame.Z + 400)
   local part = Instance.new("Part", game.Workspace)
   part.Transparency = 1
   part.Position = Vector3.new(vX, 0, vZ)
   wait(2)
   path:ComputeAsync(torso.Position, part.Position)
   local waypoints = path:GetWaypoints()
   part:Destroy()
   return waypoints

But they all end up on the same area, like shown in this video:
robloxapp-20200929-2100245.wmv (3.2 MB)
I already heard before that math.random() is not the best at generating random numbers, but is this the problem here or anything else?
I also know u would be interested into variables, so here they are:

local service = game:GetService("PathfindingService")
local hum = script.Parent.Humanoid
local torso = script.Parent.Torso
local way = torso.Position -- way doesn't play a role in this part of script

Thanks for your help!

First off sweet looking game

At the very top of your script put this line math.randomseed(tick())
This will ensure that everything is 100% random everytime.

Sometimes when everything is random oddities can occur where all the deer tend to spawn in the middle, if you are sure it is 100% a problem with the spawning and not an oddity you could try creating a visualization function that puts a red part at every spawn position so you can see where all the spawns are, you could also try dividing your map up into 9 quadrants and spawning an equal amount of dear inside each of the 9 boxes to ensure they are distributed equally thru ought the map.

2 Likes