function - VB.net choose multiple random items from arraylist only once -


so i'm trying make function can input arraylist , function randomly picks x amount of items arraylist , outputs result new arraylist. i've got working, problem i'm having after 1 of items picked randomize, it's still there means can picked again. here code got far

dim randomgeneratormulti new arraylist()     dim resultmulti new arraylist()          randomgeneratormulti.add("happy")         randomgeneratormulti.add("sad")         randomgeneratormulti.add("smart")         randomgeneratormulti.add("intelegt")         randomgeneratormulti.add("stupid")         randomgeneratormulti.add("ugly")          choosemulti("5", randomgeneratormulti)         traitslistbox.items.addrange(resultmulti.toarray) 

here function

 function choosemulti(byval numbers integer, byval alist arraylist) arraylist          dim rnd = new random()           integer = 1 numbers              resultmulti.add(alist.item(rnd.next(0, alist.count)))           next         return resultmulti      end function 

all appreciated :)

you should use list(of string) rather arraylist, have written code below using arraylist it's closer original.

you can remove items pick list:

function choosemulti(byval numbers integer, byval alist arraylist) arraylist   dim resultmulti new arraylist()   dim rnd = new random()   integer = 1 numbers     dim index integer = rnd.next(alist.count)     resultmulti.add(alist.item(index))     alist.removeat(index)   next   return resultmulti end function 

note: returning list function, should return value , put in resultmulti variable, , not change variable inside function:

resultmulti = choosemulti(5, randomgeneratormulti) 

another method shuffle list (using fisher-yates shuffle) , take first items:

function choosemulti(byval numbers integer, byval alist arraylist) arraylist   dim resultmulti new arraylist()   dim rnd = new random()   integer = alist.count - 1 1 step -1     dim index = rnd.next(i + 1)     dim temp string = alist(i)     alist(i) = alist(index)     alist(index) = temp   next   integer = 0 numbers - 1     resultmulti.add(alist.item(i))   next   return resultmulti end function 

yet approach pick numbers list start end. way there can't duplicates , original list left unchanged, side effect picked items have same order in original list:

function choosemulti(byval numbers integer, byval alist arraylist) arraylist   dim resultmulti new arraylist()   dim rnd = new random()   dim index integer = 0   while numbers > 0     if rnd.next(alist.count - index) < numbers       resultmulti.add(alist.item(index))       numbers -= 1     endif     index += 1   wend   return resultmulti end function 

Comments

Popular posts from this blog

mysql - FireDac error 314 - but DLLs are in program directory -

git - How to list all releases of public repository with GitHub API V3 -

c++ - Getting C2512 "no default constructor" for `ClassA` error on the first parentheses of constructor for `ClassB`? -