How do i make table choose 16 different variants of number, but NOT a specific number

Ok, so im tryna do that table chooses 16 variants of number of 8 numbers radius of specific number but NOT the specific number using math.random. Unfortunately math.random can choose the number i dont want twice and idk how to prevent that this is my script, where chosen.answer is the specific number, if you know please help me, i would also like if you knew how to make it that there are never same numbers in the table

for i = 1, 16 do
			table.insert(wronganswers, math.random(chosen.answer - 8, chosen.answer + 8))
		end
		
for i, v in pairs(wronganswers) do
	if v == chosen.answer then
		table.remove(wronganswers, v)
		end
	end
		
print(chosen.answer)
print(wronganswers)
1 Like

i put this in less than 2 min might not work:

local wronganswers = {}

for i = 1, 16 do
    local randomNumber
    repeat
        randomNumber = math.random(chosen.answer - 8, chosen.answer + 8)
    until randomNumber ~= chosen.answer and not table.find(wronganswers, randomNumber)
    
    table.insert(wronganswers, randomNumber)
end

print(chosen.answer)
print(wronganswers)

function table.find(tbl, value)
    for i, v in ipairs(tbl) do
        if v == value then
            return i
        end
    end
    return nil
end

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.