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

Why are these being used more than once?

Asked by
ItsMeKlc 235 Moderation Voter
8 years ago

I want each letter to only be used once when it prints on the last line, but it does some letters more than once, why?

    local Pool={"a","b","c"}
    Pool[1] = Pool[math.random(1,#Pool)]
    local OM1 = Pool[1]
    table.remove(Pool, 1)

    Pool[1] = Pool[math.random(1,#Pool)]    
    local OM2 = Pool[1]
    table.remove(Pool, 1)

    Pool[1] = Pool[math.random(1,#Pool)]    
    local OM3 = Pool[1]
    table.remove(Pool, 1)

    print(OM1,OM2,OM3)

1 answer

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

Each time, you're replacing the first element with a random one. This eliminates the first one at each step from being truly chosen later. You then eliminate that copy instead of the original.

That will look like this:

{a, b, c}
{c, b, c} -- Pool[1] = Pool[3]
{b, c} -- table.remove(Pool, 1)
{c, c} -- Pool[1] = Pool[2]
{c} -- table.remove(Pool, 1)

Here is how you remove a random thing from a list:

local O1 = table.remove(Pool, math.random(#Pool))

Note that the 1, is optional in math.random.

0
I'm sorry, but where would I use this? I don't quite understand ItsMeKlc 235 — 8y
0
Sorry, had a typo in the correct line. It's also a little clearer now. BlueTaslem 18071 — 8y
0
I still don't understand... Do I replace my lines 4,8,12 with this? ItsMeKlc 235 — 8y
0
I give a line explaining how to get and remove a random thing from a list. It defines `O1`. You can infer that would replace the stuff you use to modify Pool and get OM1. You can then infer that since `1` is not part of it, you can reuse this same line to make OM2 and OM3. BlueTaslem 18071 — 8y
0
Oh okay. Thank you so much! ItsMeKlc 235 — 8y
Ad

Answer this question