title: VB: Random Shuffler Duplicating Values
body: <p>I am trying to make a Deal or no Deal game, however my problem at the moment is assigning set values to cases randomly. I have had no luck with randomly generating a number and checking if it already exists, so I am now attempting to simply shuffle the array. The problem is, for some reason, values get duplicated.
  The code:</p>
  
  <pre><code> Dim nCaseValues(26) As Integer 'The different possible values for a case
      Dim nCaseNumbers(26) As Integer 'The different case numbers
  
      Dim nShadowNumber As Integer 'This holds the first number in the shuffle
      Dim nShuffleNumber1 As Integer 'The first random position
      Dim nShuffleNumber2 As Integer 'The second random position
  
      'Clear the list if it already has content
      listArray1.Items.Clear()
      listArray2.Items.Clear()
      listArray3.Items.Clear()
  
      'Declare array for case values
      nCaseValues(0) = 1500
      nCaseValues(1) = 1
      nCaseValues(2) = 2
      nCaseValues(3) = 5
      nCaseValues(4) = 10
      nCaseValues(5) = 20
      nCaseValues(6) = 50
      nCaseValues(7) = 100
      nCaseValues(8) = 150
      nCaseValues(9) = 200
      nCaseValues(10) = 250
      nCaseValues(11) = 500
      nCaseValues(12) = 750
      nCaseValues(13) = 1000
      nCaseValues(14) = 2000
      nCaseValues(15) = 3000
      nCaseValues(16) = 4000
      nCaseValues(17) = 5000
      nCaseValues(18) = 10000
      nCaseValues(19) = 15000
      nCaseValues(20) = 20000
      nCaseValues(21) = 30000
      nCaseValues(22) = 50000
      nCaseValues(23) = 75000
      nCaseValues(24) = 100000
      nCaseValues(25) = 200000
  
      'Declare array for case numbers
      For genCaseNumArray = 0 To 25
          nCaseNumbers(genCaseNumArray) = 0
          listArray1.Items.Add(genCaseNumArray)
      Next
  
  'The shuffle
      For J = 0 To 25 'This assigns case values to case numbers (It's probably moot, as I can just use the nCaseValues position, but it's here for now)
          nCaseNumbers(J) = nCaseValues(J)
      Next
      For K = 0 To 25 'Lists the items below, this could possibly be an error
  
          For I = 0 To 50 'Shuffles the list 50 times
              nShuffleNumber1 = (Int(Rnd() * 26)) 'Gets a random number and assigns it
              nShuffleNumber2 = (Int(Rnd() * 26))'Gets a random number and assigns it
              nShadowNumber = nCaseNumbers(nShuffleNumber1) 'This holds the first value during the shuffle
              nCaseNumbers(nShuffleNumber1) = nCaseNumbers(nShuffleNumber2)'First value now equals second value...
              nCaseNumbers(nShuffleNumber2) = nShadowNumber 'And now second value holds first value.
  
          Next 'Lists items in lists objects on the form
          listArray1.Items.Add(K)
          listArray2.Items.Add(nCaseValues(K))
          listArray3.Items.Add(nCaseNumbers(K))
      Next
  </code></pre>
  
  <p>I need each value to be unique. I am pretty sure it is a logic error in my code. Any help on why the numbers are duplicating would be great.</p>
  
  <p>Here are a couple of outputs:</p>
  
  <p><a href="https://i.stack.imgur.com/g36bq.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/g36bq.png" alt="Outcome #1"></a>
  <a href="https://i.stack.imgur.com/mmsP5.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/mmsP5.png" alt="Outcome #2"></a></p>
  
  <p>The first list is the Case Number, second is possible values in order, third is the final outcome - The individual case value. As you can see, some are duplicated, and the first list is repeated twice..
  EDIT: I found out why it repeated twice - I was stupid enough to leave another for loop that was also adding to the list. I have solved my problem, see the answer below.</p>