ID:392508
 
Keywords: list, pick, random
(See the best response by Nadrew.)
Code:
                    ReRoll
var/mob/A in pick(AutoTournamentList1)
var/mob/B in pick(AutoTournamentList1)
if(A==B)
goto ReRoll


Problem description:

OK i have a list full of mobs, i want to automatically select 2 different mobs off this list and i am having a little trouble figuring out how to do it.

This is how i think it can work however i have not tested it yet but perhaps some of you have tested this and think it will work :D.
You can use the pick() proc to select the first mob from the list, then you can remove the chosen mob from the list and re-roll and select another mob from the list.
var/list/L += AutoTournamentList1 //create a new and add the players to it the list
var/list/C = list() //create another list(these will be for the ones that were choosed)
for(var/i = 1, i <= 2, i++)//loop 2 times
if(L.len < 1) break//if there are no players, break the cycle
var/mob/G = pick(L)//if there is enough players grab one
C.Add(G)//add it to the chosen list
L.Remove(G)//remove it from our players list
if(C.len > 1) return 1 //if theres more than 2 players in the chosen list choosing 2 random mobs was a success.

using a loop to check like that isn't such a good idea. try something like this:

// l is your list
// count is how many random items you want from that list

proc/pickrand(l[],count)
.=new/list
while(l.len && count>0)
var/i = rand(1, l.len)
. += l[i]
l.Swap(i,l.len)
--l.len
--count


[EDIT] Bleh, I was beat to it :p. But still, I think this method may be faster

[EDIT 2] a small change, notsure why i was Swap()ing :/:
proc/pickrand(l[],count)
.=new/list
while(l.len && count>0)
var/i = rand(1, l.len)
. += l[i]
l.Cut(i,i+1)
--count


[EDIT 3] i was thinking of something like this with the first one, but botched it up
proc/pickrand(l[],count)
.=new/list
while(l.len && count>0)
var/i = rand(1, l.len)
. += l[i]
l[i]=l[l.len]
--l.len
--count
hrm not sure i never use the while loop unless it was something big. forum account may know which is faster.
Best response
Doesn't even have to be that complicated.
var/first_element = pick(my_list)
var/second_element = pick((my_list-first_element))
would if the list was empty would'nt that create a runtime error?
That was just an example, you obviously should be adding some sanity checks to make sure things don't become empty or null.
Ah ty for all the help i figured it out no need for the 'in'