1. Computing

Create a Random Collectionof Unique Integers

The Complete Code

From , former About.com Guide

This is page 2 of an article about how to create a random array of unique integers. This is a requirement for most card games and for many other applications as well.

This program is a followup to an earlier one using arrays. You can find the earlier article here. This one shows how to use collections and a Lambda expression to do the job, but it also contrasts that with older methods. Thanks to John McIlhinney for the new code in this example. The program is explained on page 1.

--------
Click Here to display the illustration
--------

And here's the source source.


Public Class ArraySize
    Dim theSize As Integer
    Dim Cards() As Integer
    Dim TotalTime As TimeSpan
    Dim theStopWatch As New Stopwatch
    Dim iterations As Integer
    Private Sub GenArrayChecking_Click(
        ByVal sender As System.Object,
        ByVal e As System.EventArgs
        ) Handles GenArrayChecking.Click
        For Me.theSize = CInt(StartingArraySize.Text) _
            To CInt(MaximumArraySize.Text) _
            Step CInt(ArraySizeIncrement.Text)
            theStopWatch.Reset()
            theStopWatch.Start()
            For Me.iterations = 1 To CInt(IterationCount.Text)
                'Dim CardDeck =
                '    Enumerable.Repeat(-1, Me.theSize).ToList
                Cards =
                    Enumerable.Repeat(-1, Me.theSize).ToArray()
                Dim Tmp As Integer
                Randomize()
                Dim i As Integer = 0
                Do While i < Me.theSize
                    Tmp = Int(Me.theSize * Rnd())
                    If Array.BinarySearch(Cards, Tmp) < 0 Then
                        'If Not CardDeck.Contains(Tmp) Then
                        Cards(i) = Tmp
                        'CardDeck(i) = Tmp
                        i += 1
                    End If
                Loop
                ' Comment this loop when running time trials
                ' Updating the Listbox takes more time than anything
                For i = 0 To Me.theSize - 1
                    UniqueCards.Items.Add(Cards(i))
                Next
            Next
            theStopWatch.Stop()
            'Texboxes were used here because you can't copy and paste from
            'a Listbox and I wanted to graph the results in Excel.
            AverageTime.Text = theStopWatch.ElapsedMilliseconds / iterations
            ArraySizeList.Text &= (vbNewLine & theSize)
            AverageTimeList.Text &= (vbNewLine & AverageTime.Text)
        Next
    End Sub

    Private Sub GenArrayShuffling_Click(
        ByVal sender As System.Object,
        ByVal e As System.EventArgs
        ) Handles GenArrayShuffling.Click
        For Me.theSize = CInt(StartingArraySize.Text) _
            To CInt(MaximumArraySize.Text) _
            Step CInt(ArraySizeIncrement.Text)
            theStopWatch.Reset()
            theStopWatch.Start()
            For Me.iterations = 1 To CInt(IterationCount.Text)
                Dim Tmp As Integer
                Dim ReplaceLocation As Integer
                Dim i As Integer
                Dim CardDeck = Enumerable.Range(1, theSize).ToList
                'Dim Cards() =
                '    Enumerable.Repeat(0, theSize).ToArray()
                'For i = 0 To theSize - 1
                '    Cards(i) = i + 1
                'Next
                For i = 0 To theSize - 1
                    ReplaceLocation = Int(theSize * Rnd())
                    Tmp = CardDeck(i)
                    'Tmp = Cards(i)
                    CardDeck(i) = CardDeck(ReplaceLocation)
                    'Cards(i) = Cards(ReplaceLocation)
                    CardDeck(ReplaceLocation) = Tmp
                    'Cards(ReplaceLocation) = Tmp
                Next
                ' Comment this loop when running time trials
                ' Updating the Listbox takes more time than anything
                'For i = 0 To theSize - 1
                '    UniqueCards.Items.Add(Cards(i))
                'Next
            Next
            theStopWatch.Stop()
            AverageTime.Text =
                theStopWatch.ElapsedMilliseconds / iterations
            ArraySizeList.Text &= (vbNewLine & theSize)
            AverageTimeList.Text &= (vbNewLine & AverageTime.Text)
        Next
    End Sub

    Private Sub GenArrayLambda_Click(
        ByVal sender As System.Object, ByVal e As System.EventArgs
        ) Handles GenArrayLambda.Click
        ' Converted from the example provided by John McIlhinney
        For Me.theSize = CInt(StartingArraySize.Text) _
            To CInt(MaximumArraySize.Text) _
            Step CInt(ArraySizeIncrement.Text)
            theStopWatch.Reset()
            theStopWatch.Start()
            For Me.iterations = 1 To CInt(IterationCount.Text)
                'Create the deck.
                Dim CardDeck = Enumerable.Range(1, Me.theSize)
                Dim rng As New Random
                'Shuffle the deck.
                CardDeck = CardDeck.OrderBy(Function(n) rng.Next)
                Cards = CardDeck.ToArray
            Next
            ' Comment this loop when running time trials
            ' Updating the Listbox takes more time than anything
            'For i = 0 To Me.theSize - 1
            ' UniqueCards.Items.Add(Cards(i))
            'Next
            theStopWatch.Stop()
            AverageTime.Text =
                theStopWatch.ElapsedMilliseconds / iterations
            ArraySizeList.Text &= (vbNewLine & theSize)
            AverageTimeList.Text &= (vbNewLine & AverageTime.Text)
        Next
    End Sub

    Private Sub ClearStatistics_Click(
        ByVal sender As System.Object, ByVal e As System.EventArgs
        ) Handles ClearStatistics.Click
        ArraySizeList.Text = ""
        AverageTimeList.Text = ""
    End Sub

End Class
  1. About.com
  2. Computing
  3. Visual Basic
  4. Using VB.NET
  5. Collections of Unique Integers For Dealing Cards in Games>

©2013 About.com. All rights reserved.