Results 1 to 2 of 2

Thread: Validate Output results

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Dec 2003
    Posts
    127

    Validate Output results

    I'm currently using the code below to create random numbers from a button click, I need something in my code to ensure that numbers are not duplicated. For example the range is 0 to 30 picking 3 numbers in 1 play. It should give results like 1-2-3 for example and not duplicate any number like 1-2-2 so on and so forth. Anyone have an example of how I can do this?

    Thanking all replies in advance!!



    Code:
    Public Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    Dim minRange = 0
                Dim maxRange = 30
                Dim numsPerLine = 3
                Dim numLines = 1
    
                Dim i As Integer
                Dim j As Integer
                Dim results() As Integer
                Dim output As String
                ListBox3.Items.Clear()
                'Get known
                Dim known As ListBox
                known = knownValuesList
                'Array keeping the results
                ReDim PResults(numLines, numsPerLine)
                For i = 1 To numLines
                    results = getNumbers(known, numsPerLine, minRange, maxRange)
                    For j = 0 To results.GetUpperBound(0)
                        PResults(i - 1, j) = results(j)
                        'Can change the "0" to " " to use a space
                        Dim temp As String = IIf(CheckBox1.Checked = True, String.Format("{0:00}", results(j)), CStr(results(j)))
                        If output <> "" Then output += " - "
                        output += temp
                    Next
                    ListBox3.Items.Add(output)
                    output = ""
                Next
    End Sub
    
    ' Used to shuffle / randomize the output
        ' Use this code in any event handle to activate the shuffle
        ' ShuffleListbox(KnownValueList)
        Public Sub ShuffleListbox(ByVal lb As ListBox)
            Dim i As Integer
            Dim temp As Object
            Dim r As New Random
            Dim rndIndex As Integer
            Dim lbCount As Integer = lb.Items.Count
    
            lb.BeginUpdate()
            For i = 0 To lbCount - 1
                temp = lb.Items(i)
                rndIndex = r.Next(0, lbCount)
                lb.Items(i) = lb.Items(rndIndex)
                lb.Items(rndIndex) = temp
            Next
            lb.EndUpdate()
        End Sub
    
    Public Function getNumbers(ByVal known As ListBox, ByVal requiredNumber As Integer, ByVal minRange As Integer, ByVal maxRange As Integer) As Integer()
    
            Dim output() As Integer
            Dim i As Integer
            Dim tmp As Integer
            Dim rndIndex As Integer
            Dim tmpArray(requiredNumber - 1) As Integer
            Dim randomPool As New ArrayList
    
            ' build the random pool
            For i = minRange To maxRange
                ' don't include any of the known values
                If Not known.Items.Contains(i) Then
                    randomPool.Add(i)
                End If
            Next
    
            ' shuffle the random values
            For i = 0 To (randomPool.Count - 1)
                rndIndex = R.Next(0, randomPool.Count)
                tmp = randomPool(i)
                randomPool(i) = randomPool(rndIndex)
                randomPool(rndIndex) = tmp
            Next
    
            ' populate tmpArray with the known values
            ' and fill in any remaining slots
            ' with values from the shuffled randomPool
            For i = 0 To (requiredNumber - 1)
                If i <= (known.Items.Count - 1) Then
                    tmpArray(i) = known.Items(i)
                Else
                    tmpArray(i) = randomPool.Item(0)
                    randomPool.RemoveAt(0)
                End If
            Next
    
            ' shuffle the set by swapping
            ' each position with another
            For i = 0 To tmpArray.GetUpperBound(0)
                rndIndex = R.Next(0, tmpArray.GetUpperBound(0))
                tmp = tmpArray(i)
                tmpArray(i) = tmpArray(rndIndex)
                tmpArray(rndIndex) = tmp
            Next
            output = tmpArray
            Return output
        End Function
    using: VB.Net 2005

  2. #2
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    105,437

    Re: Validate Output results

    I have provided code to do just that in the Codebank.

    http://www.vbforums.com/showthread.php?t=393023

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width