Trying to make random number less than 90% of magnitude

i don’t think there’s many topics on this but my script spawns parts with a random offset along a part’s look vector. im trying to make it so that my last part that is spawned will be positioned at less or equal to 90% of the magnitude so that it won’t be overlapping my neon brick. i just dont really understand what i’m doing but i appreciate any help ty. :sob:

the magnitude is like 24 between my green neon parts (b1 and b2) and 90% of that is 21.6.


local function randomNumber()

	for i = 1,10 do
		local random = Random.new()
		local randomNumber = random:NextNumber(1.5, b1b2Distance/8)

		if lastnumber >= (b1b2Distance / 10) * 9 then --if the lastnumber is greater or equal to 90% of the magnitude then make it less than that
			repeat
				randomNumber = random:NextNumber(0.5, b1b2Distance/8)
				wait(.25)
			until lastnumber < (b1b2Distance / 10) * 9 --90% of the magnitude
		end

		lastnumber += randomNumber
		print(i..": "..lastnumber)

		local part = Instance.new("Part", b1)
		part.Anchored = true
		part.Size = Vector3.one
		part.BrickColor = BrickColor.random()
		part.CFrame = b2.CFrame + b2.CFrame.LookVector * Vector3.new(0,0,lastnumber)

		wait(.25)
	end

end

randomNumber()

One thing to try here is whether you can change the distance in which it can go to the next number. I have not worked much with Random.new() but from what I can tell, line 5 of your excerpt here makes “randomNumber” be equal to some new number between 1.5 and (distance/8).

If the b1b2Distance variable is equal to the magnitude between the two parts, can you artificially reduce that number? Right now you indicate it would be equal to 24, but could you make b1b2Distance equal to ((b1.Position - b2.Position).Magnitude) - 4) (you can play around with the 4 here, to find an number which is good for you.

If you are working with multiple different distances, you should find a way to incorporate this logic into your code. The way you do it will be slightly different, obviously, but the concept is the same: reduce the amount of space that the part has to generate in.

1 Like

thats absolutely perfect mate, spot on.

i didn’t think of this at all until you told me.

all i did was i altered the magnitude like u said and it seems to be working out perfectly atm and the part isnt overlapping the neon block, thanks a million man!! :slight_smile:

local b1b2Distance = ((b1.Position - b2.Position).Magnitude) - 3.5


image

1 Like

Had this same issue months so and solved it the same way @notsfeelings solved your problem. Just a tip, I suggest calculating exactly the amount your random range calculation result should be reduced based off doing math with the part’s size(like subtracting half its X-axis size from the distance range). That way in this case and future ones you get into the habit of preventing this sort of problem before it materializes. In the process you’ll also save yourself some guesswork trying to find out how much to reduce ranges

(Not a solution; just a suggestion)

1 Like

honestly, i would love to get the exact random range but this was like my first time doing this sort of thing so i decided to try different things until i got something going.

nonetheless, i’m happy to take any suggestion that comes my way Tenny, thanks a million for your help mate :slight_smile:

1 Like