Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
2

How to do math.random without pulling specific number?

Asked by 4 years ago

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

1math.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

Log in to vote
0
Answered by 4 years ago
Edited 4 years ago

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

01local randomNumber = math.random(1,10)
02local blacklist = {5,7,3} -- put numbers to exclude here
03 
04local fails = {}
05local good = false
06local oof = false
07 
08repeat
09    for x = key,number in pairs(blacklist) do
10        if randomNumber == number then -- if the random number is in blacklist
11            table.insert(fails,false)
12            continue
13        end
14 
15        if not randomNumber == number then -- if the random number is not in blacklist
View all 26 lines...
0
..that's a terrible way to do it, but good thinking. Fifkee 2017 — 4y
0
yea im bad at scripting lol ProjectInfiniti 192 — 4y
Ad
Log in to vote
0
Answered by
Fifkee 2017 Community Moderator Moderation Voter
4 years ago
Edited 4 years ago

Don't accept this answer.

1local math = setmetatable({}, {__index = math});
2 
3function math.random(a,b, blacklist)
4    local randomInt = getmetatable(math).__index.random(a,b);
5    if not (table.find(blacklist, randomInt)) then
6        return randomInt;
7    end
8    return math.random(a,b, blacklist)
9end

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.

0
So this is how we extend Lua's library. Cool! Block_manvn 395 — 4y
Log in to vote
0
Answered by 4 years ago

people are putting wayyy to complicated stuff

01local function check(tbl, thing)
02    for _, v in pairs(tbl) do
03        if thing == v then
04            return true
05        end
06    end
07end
08local function randomwithexclude(start, end, excluded)
09    local timesdone = 0
10    local num
11    repeat
12        num = math.random(start, end)
13        timesdone = timesdone + 1
14    until check(excluded, num) ~= true or timesdone == 500 -- returns nil if the function runs 500 times
15    if timesdone == 200 then
View all 22 lines...

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

Log in to vote
0
Answered by 4 years ago
Edited 4 years ago

Hi!

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

Solution 1: Reroll Until Not 5

01local chosenNumber = math.random(1,10)
02 
03while true do
04    if chosenNumber == 5 then
05        chosenNumber = math.random(1,10)
06    else
07        break
08    end
09    wait()
10end

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

1local avaliableNumbers = {1,2,3,4,6,7,8,9,10}
2local 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.

0
Solution 1 is terrible, don't do it. Solution 2 is too barebones, but it's on the right track. Fifkee 2017 — 4y
0
Ok, thanks for the advice. LennyPlayzYT 269 — 4y

Answer this question