0

comList is a list of strings (let us say 100), I want to draw eight strings out of the list randomly and save them into a new list. This is what I did.

Dim rnd As New Random()            

For i As Integer = 0 To 7
   _rndString.Add(comList.Item(rnd.Next(1, 8)))
Next

Looking into the new list is saw duplicates. That is since the Random Class draws with replacement.

Let us say I have 4 strings: a,b,c,d and I draw 3 randomly I can get the following resultset:

a,a,a

What is happening her. I draw a and a is put back into the pool of strings, so it is possible that I draw it again (shuffeling the pool doesnt help here since I still have a possibility to draw the same string). That is what rnd.Next(1,8) does, it draws a random number up to 8 but in the next draw you can draw it again.

I checked the Random Class but couldnt find a method for drawing without replacement. Does anyone knows if there is a Class in .NET that implements a drawing without replacement or how I can get this done?

ruedi
  • 5,365
  • 15
  • 52
  • 88
  • 1
    Drawing without replacement is a [shuffle](http://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle), not a random number. – Dour High Arch Dec 01 '14 at 17:19
  • Sorry I am not getting it. The solution in the link is as follows: I have a Array with 5 items put this into the function and get back an array where the items are randomly sorted. Ok, but I have 100 strings and have to draw 7 and these 7 have to be drawn without replacement. I just dont see how the shuffling can help me. – ruedi Dec 01 '14 at 17:55
  • Shuffle 100 strings. Draw 7. If that is not what you mean, edit your question to explain what you want, why a shuffle does not help and your question will be reopened. – Dour High Arch Dec 01 '14 at 18:02
  • ok, I revised my question. – ruedi Dec 01 '14 at 18:11
  • What you are describing is shuffling, not `Random`, as posted before. Shuffling does help because it does not not “still have a possibility to draw the same string”; if that is what you are getting you are not shuffling. Please post your code. – Dour High Arch Dec 01 '14 at 18:28
  • Ah, I shuffle the 100 strings and just take the first 8 and get what I want. Sorry it is late! Thanks for pushing me in the right direction! – ruedi Dec 01 '14 at 18:36

0 Answers0