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

How do you check to see if randomly picked things arent repeating?[Solved]

Asked by
parkderp1 105
10 years ago

I'm trying to make a script that picks two random things and then make sure they do not repeat. I currently have:

admin={"parkderp1","random","name"}
for i=2, do
pick=admin[math.random(1,#admin)]
wait(0.01)
print(pick)
end

Thats working i just dont know how to check if they repeat by repeating i mean the output being: parkderp1,parkderp1

1 answer

Log in to vote
1
Answered by
BlueTaslem 18071 Moderation Voter Administrator Community Moderator Super Administrator
10 years ago

A really simple way is to keep re-picking until the choice is not what it was the time before, e.g.,

local previousChoice = nil

local list = {"abc","def", "ghi"}
while true do
    wait(1)
    local choice
    repeat
        choice = list[ math.random(1, #list) ]
    until choice ~= previousChoice
    previousChoice = choice

    print(choice)
end

Warning: If there's only one element in the list, this will cause an infinite loop.

0
Thank you it seems to be working. parkderp1 105 — 10y
Ad

Answer this question