Posts ID:392508 Favorites
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.
 #1 Feb 27 2012, 9:22 pm (Edited on Feb 27 2012, 9:55 pm) 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 listvar/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 listif(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 listproc/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 ```
 #4 Feb 27 2012, 9:46 pm 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)) ```
 #6 Feb 28 2012, 2:00 am would if the list was empty would'nt that create a runtime error?
 #7 Feb 28 2012, 11:33 am That was just an example, you obviously should be adding some sanity checks to make sure things don't become empty or null.
 #8 Feb 28 2012, 12:07 pm Ah ty for all the help i figured it out no need for the 'in'