Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

How to do math.random without pulling specific number?

For example, lets say I want to pull a number from 1 - 10. That would be:

math.random(1,10)

However, how would I be able to randomize a number from 1 - 10 but exclude a specific number, like 5?

4 answers

make a blacklist that contains the numbers that you want to exclude, then iterate through the blacklist to check the random number

local randomNumber = math.random(1,10)
local blacklist = {5,7,3} -- put numbers to exclude here

local fails = {}
local good = false
local oof = false

repeat
	for x = key,number in pairs(blacklist) do
		if randomNumber == number then -- if the random number is in blacklist
			table.insert(fails,false)
			continue
		end

		if not randomNumber == number then -- if the random number is not in blacklist
			continue
		end
	end

	if #fails == 0 then -- if the random number is not in blacklist
		print("Number is good!")
		good = true
	if not #fails == 0 then --  if the random number is blacklisted
		print("Number is bad oof")
		oof = true
until good or oof

2 comments

0

..that's a terrible way to do it, but good thinking.

0

yea im bad at scripting lol

Don't accept this answer.

local math = setmetatable({}, {__index = math});

function math.random(a,b, blacklist)
	local randomInt = getmetatable(math).__index.random(a,b);
	if not (table.find(blacklist, randomInt)) then
		return randomInt;
	end
	return math.random(a,b, blacklist)
end

Not to be used in excessiveness, like all random functions. Not performant either, but Jiramide will probably come and fix it for me. This is a better alternative to ProjectInfiniti's answer, so please accept his answer. He had the right idea and the right execution.

1 comment

0

So this is how we extend Lua's library. Cool!

people are putting wayyy to complicated stuff

local function check(tbl, thing)
	for _, v in pairs(tbl) do
		if thing == v then
			return true
		end
	end
end
local function randomwithexclude(start, end, excluded)
	local timesdone = 0
	local num
	repeat
		num = math.random(start, end)
		timesdone = timesdone + 1
	until check(excluded, num) ~= true or timesdone == 500 -- returns nil if the function runs 500 times
	if timesdone == 200 then
		return nil
	else
		return num
	end
end

print(randomwithexclude(1, 10, {3, 6, 9})) -- never tested it but it must be a table with values inside

not way too sure if it works. u could replace the timesdone with something else

Hi!

People are creating really complicated answers! I have two very simple solutions:

Solution 1: Reroll Until Not 5

local chosenNumber = math.random(1,10)

while true do
	if chosenNumber == 5 then
		chosenNumber = math.random(1,10)
	else
		break
	end
	wait()
end

This simply chooses a number between 1 and 10. If the result is 5, it will keep on rerolling until it is not 5. Once the variable "chosenNumber" is not 5, the while loop will break.

Solution 2: Random From Table

local avaliableNumbers = {1,2,3,4,6,7,8,9,10}
local chosenNumber = avaliableNumbers[math.random(1, #avaliableNumbers)]

avaliableNumbers is a table that has the numbers 1-10 except for 5. Line 2 chooses a random value from the table.

2 comments

0

Solution 1 is terrible, don't do it. Solution 2 is too barebones, but it's on the right track.

0

Ok, thanks for the advice.

This discussion is preserved as it was. New answers, comments, and votes are closed.