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

Using a conditional statement with math.random?

Asked by 6 years ago
Edited 6 years ago

For example, the code below.

function chooseRandom()
    local random = table[math.random(1, #table)]
    return random
end

How can I check if the selected random value is "Test", for example? If it is Test, return the value... if not, choose another random value until it meets that condition.

Check the comments for another example.

There's probably a simple way to do this that I'm missing. Does anyone know a solution to accomplish this?

0
Then why randomize it if you only want one option to be picked? hiimgoodpack 2009 — 6y
0
I'm just using that as an example. Lets say there is a BoolValue associated with each value in the table. I need to check if the value is true. If it is, return the value... if not, choose another random value until it is true. ViciousViperV2 24 — 6y
0
if random == true then return random else return nil???? abnotaddable 920 — 6y

1 answer

Log in to vote
0
Answered by 6 years ago
Edited 6 years ago
function chooseRandom()
    local Selected --store our value for selected value
    repeat Selected = table[math.random(1,#table)] until Selected == "Test"
    return Selected
end
Ad

Answer this question