Need help with spread script

I have made a script that applies spread to a bullet but the problem here is I want it based on your character’s distance to the closest object infront. It should work like this:

The further away the object the more spread becomes, it can’t go beyond already specified spread
The closer the object is the more accurate the bullets are

Currently it just makes the further away objects have better accuracy than the objects closer to you

	local Hit = Utility:CastRay(CameraRay.Origin, CameraRay.Direction * 2500, false, IgnoreList)
		local Position = CameraRay.Direction * 25000
		if Hit then
			Position = Hit.Position - CameraRay.Origin
		local Magnitude = (WeaponModel.Muzzle.Position - Position).Magnitude

	    Position = Position + * (math.random(-GunSetting.Spread, GunSetting.Spread) / 600), (Magnitude * (math.random(-GunSetting.Spread, GunSetting.Spread)) / 600), (Magnitude * (math.random(-GunSetting.Spread, GunSetting.Spread) / 600)))

		local Cast = caster:Fire(WeaponModel.Muzzle.Position, (CameraRay.Origin + Position) - WeaponModel.Muzzle.Position, 500, castBehaviorLocal)
		Cast.UserData.Local = true
Magnitude * math.random(-GunSetting.Spread, GunSetting.Spread)

The thing is, math.random(-GunSetting.Spread, GunSetting.Spread) is still drawing from the same distribution regardless of distance, and then all it does is scale the magnitude by that number. So if you want it to have a higher probability to spread out the further an object is, maybe try something like:

math.random(-magnitude, magnitude)

or whatever variant of this.

But how will it then distribute the spread for longer distances if I don’t tell it the magnitude

I’m not sure what you mean by this because I didn’t say anything about not telling it the magnitude.