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?
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?
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
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.
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.
Solution 1 is terrible, don't do it. Solution 2 is too barebones, but it's on the right track.
Ok, thanks for the advice.
This discussion is preserved as it was. New answers, comments, and votes are closed.
2 comments
..that's a terrible way to do it, but good thinking.
yea im bad at scripting lol