How would you make math.random go up in a sequence instead of all over the place?

so im working on a lightning strike ig and ive created 10 attachments placed at random along the starting point’s look vector. (i appreciate any help btw ty!!)

how would i make it so that math.random goes up in random numbers say like:
1, 6, 9, 16, 24

instead of:
24, 14, 20, 7, 5, 3, 17

here’s my output:
image

if u look here, the attachments are spawning randomly but id like it to spawn in like a straight line:
https://gyazo.com/b6e94bf45be8fa74db6868cbd15aef35

script:

local function spawnAttachments()
	
	for attachmentVariable = 1, 10 do
		local randomNumber = math.random(1,b1b2Distance)
		
		if table.find(randomNumberTable, randomNumber) then
			repeat
				randomNumber = math.random(1,b1b2Distance)
			until table.find(randomNumberTable, randomNumber) == nil
		end
		
		if table.find(randomNumberTable, randomNumber) == nil then
			table.insert(randomNumberTable, randomNumber)
		end
		
		local newAttachment = Instance.new("Attachment", b1)
		newAttachment.Name = "attachment"..attachmentVariable
		newAttachment.CFrame = b1a.CFrame + b1a.CFrame.LookVector * Vector3.new(0,0,randomNumber)
		beam.Attachment0 = newAttachment
		print(attachmentVariable..": ".. randomNumber)
		wait(.1)
		if attachmentVariable == 10 then
			beam.Attachment0 = b1a
		end
	end
	
end

spawnAttachments()

Set the generated randomNumber as the minimum value for the next math.random() calculation.

local lastNumber = 1 -- start with 1
for attachmentVariable = 1, 10 do
        local randomNumber = math.random(lastNumber, b1b2Distance)
        lastNumber = randomNumber -- next randomNumber will now be higher than previous randomNumber
        [...]
end
2 Likes

Repurpose that numbertable to store the attachment Z pos. EX:

local randomNumber = math.random(1,b1b2Distance)
		
if randomnumbertable[#randomnumbertable] > randomnumber then
	repeat
	 randomNumber = math.random(1,b1b2Distance)
	until randomnumbertable[#randomnumbertable] < randomnumber
end
table.insert(randomNumberTable, randomNumber)

The reason I’m checking the last number is because insert just adds a value at the back of the table

that’s perfect mate. thanks a million, it worked :slight_smile:

hey Dr,
thanks a million for your help. i got an error earlier; i assume im doing something wrong but ur post was very informative nonetheless, thanks a million man. :slight_smile:

i’m not sure if u meant it to be like this:

local function spawnAttachments()

	wait(1)

	for attachmentVariable = 1, 10 do
		local randomNumber = math.random(1,b1b2Distance)

		if randomNumberTable[#randomNumberTable] > randomNumber then
			repeat
				randomNumber = math.random(1,b1b2Distance)
			until randomNumberTable[#randomNumberTable] < randomNumber
		end
		table.insert(randomNumberTable, randomNumber)

		local newAttachment:Attachment = Instance.new("Attachment", b1)
		newAttachment.Name = "attachment"..attachmentVariable
		newAttachment.CFrame = b1a.CFrame + b1a.CFrame.LookVector * Vector3.new(0,0,randomNumber)
		beam.Attachment0 = newAttachment
		beam.Enabled = true
		print(attachmentVariable..": ".. randomNumber)
		wait(.1)
		beam.Enabled = false
	end
end

image

Ah my bad sry, didn’t account for empty table

local function spawnAttachments()

	wait(1)

	for i = 1, 10 do
		local randomNumber = math.random(1,b1b2Distance)
           table.insert(randomNumberTable, randomNumber)
		if randomNumberTable[i] > randomNumber then
			repeat
				randomNumber = math.random(1,b1b2Distance)
                           randomNumberTable[i] = randomNumber 
			until randomNumberTable[i] < randomNumber
		end

		local newAttachment:Attachment = Instance.new("Attachment", b1)
		newAttachment.Name = "attachment"..attachmentVariable
		newAttachment.CFrame = b1a.CFrame + b1a.CFrame.LookVector * Vector3.new(0,0,randomNumber)
		beam.Attachment0 = newAttachment
		beam.Enabled = true
		print(attachmentVariable..": ".. randomNumber)
		wait(.1)
		beam.Enabled = false
	end
end

Please use the one you marked as solution because it’s much,much better. The habit of overthinking is engrained in me so I come up with these complex solutions.