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

Randomize only 2 numbers instead of Number-Number? [SOLVED]

Asked by 9 years ago

Using;

math.random(1-3)

Would give you an output of;

1,2, or3

How would I make it so it only does two number giving an output of;

1 or 3 only

I am trying to randomize L and N by doing this:

for i = 1, 6 do
    W = W..string.char(math.random(76,78))
end

But it gives me L,M, andN I only want it to give me LandN

1 answer

Log in to vote
0
Answered by 9 years ago

Well, what you can do is use math.random() to get either 1 or 2 and set the actual value based off that.

So for example:

w = ""
for i = 1,6 do
local ran = math.random(1,2)
local char = (ran==1 and 76 or 78)
w=w..string.char(char)
end

If you are confused about this line:

local char = (ran==1 and 76 or 78)

It is exactly the same as:

if ran ==1 then
char = 76
else
char = 78
end

That is what is called a conditional ternary operator.

0
I wasn't confused. But thanks for the help! I can't believe I didn't think of that. TypicalModerator 110 — 9y
Ad

Answer this question