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

Best way to get a random true/false value?

Asked by 8 years ago

I want to get either a random true or false value, but mine seems really inefficient and I'm sure there are a few much more efficient ways of doing exactly what mine does. Here's my code:

local variable
if math.random(1,2) == 1 then
    variable = true
else
    variable = false
end

My way might be the most efficient way but I don't think it is, that code is just a general way of showing what my script does, it's not the full script.

1 answer

Log in to vote
5
Answered by 8 years ago

The best way to do this can be done on 1 line, simply have the variable assigned to a condition. Here's the most efficient way:

local variable = math.random(1,2) == 1

That will give you a random true or false value each time.

Ad

Answer this question