Question Checking no two numbers are the same.

Phil1987

Member
Joined
Nov 18, 2010
Messages
9
Programming Experience
Beginner
Hello, what the following code is ment to do is generate a bunch of numbers between 1 and 49 using the while loop to check if two of these numbers are the same. Problem is two numbers are still being generated the same and I know that the numbers generated again will not match them originals so it stops the loop.

VB.NET:
        Dim temp As Integer = 0
        For a = 0 To 5
            ballbox(a) = rnd.Next(1, 49)
            bonusball = rnd.Next(1, 49)
            temp = ballbox(a)
            While (ballbox(a) = temp) 'its not going to be the same again so it breaks the while loop
                ballbox(a) = rnd.Next(1, 49)
            End While
            While (bonusball = ballbox(a))
                bonusball = rnd.Next(1, 49)
            End While
        Next

PS: I don't want to use .contains.
 
Last edited:

Phil1987

Member
Joined
Nov 18, 2010
Messages
9
Programming Experience
Beginner
Last edited:

MattP

Well-known member
Joined
Feb 29, 2008
Messages
1,206
Location
WY, USA
Programming Experience
5-10
LINQ by default uses lazy evaluation, which means that it won't process until you physically access it. The .ToArray() causes the evaluation and gives you an array as the result rather than an IEnumberable.

If you want 6 items like something(5) you'll need to .Take(6).
 

Phil1987

Member
Joined
Nov 18, 2010
Messages
9
Programming Experience
Beginner
I've just got a quick question I've been playing with LINQ but how would you do one globally such as this.

VB.NET:
        Dim nums = Enumerable.Range(1, 100) _
                   .OrderBy(Function() rnd.Next) _
                   .Take(5) _
                   .ToArray()

So that nums could later be latter search from another private sub because i click a button to create those numbers from 1 to 100. I want to search them.

VB.NET:
        For Each num In nums
            If num.Contains(num) Then
                found += 1
            End If
        Next
 
Last edited:

MattP

Well-known member
Joined
Feb 29, 2008
Messages
1,206
Location
WY, USA
Programming Experience
5-10
I'm not sure what you're trying to do with your For loop there. From your previous posts it seems like you're trying to create a lottery drawing or something similar.

I've made a short example showing using the For loop to iterate over some use selected numbers (hard coded in the example).

Once in the For loop the If statement checks to see if nums (the random array) contains the current user entered number. If the number is in the array then I'm incrementing a counter so I can tell how many matches I've got and add the value to a List(Of Integer) so I can check later what the matching values were.

The code for IntersectButton.Click show how you can use LINQ's extension method Intersect to pull out the matching values.

VB.NET:
Public Class Form1

    Dim nums As Integer()

    Private Sub GenerateRandomButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles GenerateRandomButton.Click
        Dim rnd As New Random
        nums = Enumerable.Range(1, 100) _
            .OrderBy(Function() rnd.Next) _
            .Take(6) _
            .ToArray()
    End Sub

    Private Sub ForLoopButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _
        Handles ForLoopButton.Click

        Dim matchCounter As Integer = 0
        Dim matchingValues As New List(Of Integer)

        Dim vals As Integer() = {1, 11, 22, 33, 44, 55, 66, 77, 88, 99}

        For Each v As Integer In vals
            If nums.Contains(v) Then
                matchCounter += 1
                matchingValues.Add(v)
            End If
        Next

        Select Case matchCounter
            Case 0
                MessageBox.Show("There are no matches")
            Case 1
                MessageBox.Show("There is 1 match" & _
                                Environment.NewLine & _
                                String.Format("The matching value is: {0}", matchingValues(0)))
            Case Else
                MessageBox.Show(String.Format("There are {0} matches", matchCounter) & _
                                Environment.NewLine & _
                                String.Format("The matching values are: {0}", String.Join(",", matchingValues)))
        End Select

    End Sub

    Private Sub IntersectButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles IntersectButton.Click
        Dim vals As Integer() = {1, 11, 22, 33, 44, 55, 66, 77, 88, 99}
        Dim matches = vals.Intersect(nums)

        Select Case matches.Count
            Case 0
                MessageBox.Show("There are no matches")
            Case 1
                MessageBox.Show("There is 1 match" & _
                                Environment.NewLine & _
                                String.Format("The matching value is: {0}", matches(0)))
            Case Else
                MessageBox.Show(String.Format("There are {0} matches", matches.Count) & _
                                Environment.NewLine & _
                                String.Format("The matching values are: {0}", String.Join(", ", matches)))
        End Select
    End Sub

End Class
 
Last edited:

Phil1987

Member
Joined
Nov 18, 2010
Messages
9
Programming Experience
Beginner
I'm not sure what you're trying to do with your For loop there. From your previous posts it seems like you're trying to create a lottery drawing or something similar.

Basically, all I was trying to do was generate a few numbers that where then stored. Then I could search that array for groups of numbers that are the same. If yes then how many was right.
 
Last edited:
Top Bottom