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

How to check if a number has been already used in math.random?

How to check if a number has been already used in math.random?

1 answer

✓ Accepted answer

Hey there.

I believe you are looking to go through numbers using math.random.

My best suggestion is using an arrays and storing the numbers in it.

Therefore after each number store it in the array and go on.

local myarray = {}
local x = 0
local max = 5

repeat
    local y = math.random(1,max)
    if not myarray[y] then
        print(y)
        table.insert(myarray, y, y)
        x = x + 1
    end
until x == max

In the code above it is going through numbers 1, 2, 3, 4, and 5.

We are putting the information in an array to not repeat these numbers.

Then we are printing the numbers.

This may have not been 100% the most efficient way but I hope this helps!

2 comments

0

Tranks that's exactly what I needed!

0

when you use not myarray[y] you're checking for index y in the table rather than the number

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