Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Can you skip numbers with math.random?

Can you skip numbers with math.random? Lets say I use math.random(-1,1) Is there a way I can skip 0 so I always only get -1 or 1?

2 comments

0

math.ranodm is quite straight forward.

0

Well you sure are a bunch of help

1 answer

✓ Accepted answer

Technically
No, not really.

In this particular case, you can do

math.random() > 0.5 and 1 or -1

which selects 1 or -1 with a pretty equal chance.


As a more all-round thing, you can use tables to do this. If your numbers you're skipping don't fall into some mappable rule, you can just put all the numbers you want to be able to pick into a table and select from that table instead.

local numbers = {
 6,
 24252,
 39231283,
 2
};
local selectedNumber = numbers[math.random(#numbers)];

selectedNumber would become a random number from that list.

This discussion is preserved as it was. New answers, comments, and votes are closed.