random variable not working correctly on vb.net 2005

Hi,

I have the following code: -

 Private Sub Refresh_Sums3()
        Dim i As Integer
        Dim a As Integer

        Dim TimesText As String


        For i = 1 To 12
            Dim n() As Integer = GetProblem(Difficulty_Level)

            'TimesText = n(0).ToString & " " & Chr(247) & " " & n(1).ToString & " ="
            'OR use
            TimesText = String.Format("{0} " & Chr(247) & " {1} =", n(0), n(1))
            'see how it works?



            Select Case i
                Case 1
                    lblTimes1.Text = TimesText
                    lblSum1.Text = n(2).ToString
                Case 2
                    lblTimes2.Text = TimesText
                    lblSum2.Text = n(2).ToString
                Case 3
                    lblTimes3.Text = TimesText
                    lblSum3.Text = n(2).ToString
                Case 4
                    lblTimes4.Text = TimesText
                    lblSum4.Text = n(2).ToString
                Case 5
                    lblTimes5.Text = TimesText
                    lblSum5.Text = n(2).ToString
                Case 6
                    lblTimes6.Text = TimesText
                    lblSum6.Text = n(2).ToString
                Case 7
                    lblTimes7.Text = TimesText
                    lblSum7.Text = n(2).ToString
                Case 8
                    lblTimes8.Text = TimesText
                    lblSum8.Text = n(2).ToString
                Case 9
                    lblTimes9.Text = TimesText
                    lblSum9.Text = n(2).ToString
                Case 10
                    lblTimes10.Text = TimesText
                    lblSum10.Text = n(2).ToString
                Case 11
                    lblTimes11.Text = TimesText
                    lblSum11.Text = n(2).ToString
                Case 12
                    lblTimes12.Text = TimesText
                    lblSum12.Text = n(2).ToString

            End Select
        Next

    End Sub

    'returns product, num1, num2 in array
    Private Function GetProblem(ByVal DiffMax As Integer) As Integer()

        Dim Product, Num1, Num2 As Integer
        Dim rng As New Random
        Do

            Num1 = rng.Next(1, DiffMax)
            Num2 = rng.Next(1, DiffMax)
            Product = Num1 * Num2
        Loop Until Product < DiffMax And Num1 <> 1 And Num2 <> 1

        Return New Integer() {Product, Num1, Num2}
    End Function

Basically it creates a math sum depending on the difficulty setting which lets say is 9999. It seems to produce the same sum at least 6 times over so doesn't seem to be creating the random number correctly as it has the chance to create the numbers 1 - 9999 so quite small ods of creating the same number. Is there a way to check to ensure the sum doesn't exist first and if it does it retries to get the random number.

hope this makes sense.

Best Regards
Lee
ljhodgettAsked:
Who is Participating?
 
PaulHewsConnect With a Mentor Commented:
-Added 1*s back in.
-Screen duplicates
Public Class Form1
 
 
    Dim rng As New Random
 
    Private Sub Refresh_Sums3()
        Dim i As Integer
        Dim TimesText As String
        Dim ProductUsed As New Dictionary(Of String, String)
        For i = 1 To 12
            Dim n() As Integer
            Do
                n = GetProblem(Difficulty_Level)
                TimesText = String.Format("{0} " & Chr(247) & " {1} =", n(0), n(1))
            Loop While ProductUsed.ContainsKey(TimesText)
            ProductUsed.Add(TimesText, TimesText)
 
            Select Case i
                Case 1
                    lblTimes1.Text = TimesText
                    lblSum1.Text = n(2).ToString
                Case 2
                    lblTimes2.Text = TimesText
                    lblSum2.Text = n(2).ToString
                Case 3
                    lblTimes3.Text = TimesText
                    lblSum3.Text = n(2).ToString
                Case 4
                    lblTimes4.Text = TimesText
                    lblSum4.Text = n(2).ToString
                Case 5
                    lblTimes5.Text = TimesText
                    lblSum5.Text = n(2).ToString
                Case 6
                    lblTimes6.Text = TimesText
                    lblSum6.Text = n(2).ToString
                Case 7
                    lblTimes7.Text = TimesText
                    lblSum7.Text = n(2).ToString
                Case 8
                    lblTimes8.Text = TimesText
                    lblSum8.Text = n(2).ToString
                Case 9
                    lblTimes9.Text = TimesText
                    lblSum9.Text = n(2).ToString
                Case 10
                    lblTimes10.Text = TimesText
                    lblSum10.Text = n(2).ToString
                Case 11
                    lblTimes11.Text = TimesText
                    lblSum11.Text = n(2).ToString
                Case 12
                    lblTimes12.Text = TimesText
                    lblSum12.Text = n(2).ToString
 
            End Select
        Next
 
    End Sub
 
    Private Function GetProblem(ByVal DiffMax As Integer) As Integer()
 
        Dim Product, Num1, Num2 As Integer
 
        Do
 
            Num1 = rng.Next(1, DiffMax)
            Num2 = rng.Next(1, DiffMax)
            Product = Num1 * Num2
        Loop Until Product < DiffMax
        Return New Integer() {Product, Num1, Num2}
    End Function
 
  
End Class

Open in new window

0
 
PaulHewsCommented:
Sorry, I provided the code and I see the problem.  Declaring the Random class at the form level avoids it being seeded from the system time, and avoids producing the same grouping:
Public Class Form1
    Dim rng As New Random
 
    Private Sub Refresh_Sums3()
        Dim i As Integer
        Dim a As Integer
 
        Dim TimesText As String
 
 
        For i = 1 To 12
            Dim n() As Integer = GetProblem(Difficulty_Level)
 
            'TimesText = n(0).ToString & " " & Chr(247) & " " & n(1).ToString & " ="
            'OR use
            TimesText = String.Format("{0} " & Chr(247) & " {1} =", n(0), n(1))
            'see how it works?
 
 
 
            Select Case i
                Case 1
                    lblTimes1.Text = TimesText
                    lblSum1.Text = n(2).ToString
                Case 2
                    lblTimes2.Text = TimesText
                    lblSum2.Text = n(2).ToString
                Case 3
                    lblTimes3.Text = TimesText
                    lblSum3.Text = n(2).ToString
                Case 4
                    lblTimes4.Text = TimesText
                    lblSum4.Text = n(2).ToString
                Case 5
                    lblTimes5.Text = TimesText
                    lblSum5.Text = n(2).ToString
                Case 6
                    lblTimes6.Text = TimesText
                    lblSum6.Text = n(2).ToString
                Case 7
                    lblTimes7.Text = TimesText
                    lblSum7.Text = n(2).ToString
                Case 8
                    lblTimes8.Text = TimesText
                    lblSum8.Text = n(2).ToString
                Case 9
                    lblTimes9.Text = TimesText
                    lblSum9.Text = n(2).ToString
                Case 10
                    lblTimes10.Text = TimesText
                    lblSum10.Text = n(2).ToString
                Case 11
                    lblTimes11.Text = TimesText
                    lblSum11.Text = n(2).ToString
                Case 12
                    lblTimes12.Text = TimesText
                    lblSum12.Text = n(2).ToString
 
            End Select
        Next
 
    End Sub
 
    Private Function GetProblem(ByVal DiffMax As Integer) As Integer()
 
        Dim Product, Num1, Num2 As Integer
 
        Do
 
            Num1 = rng.Next(1, DiffMax)
            Num2 = rng.Next(1, DiffMax)
            Product = Num1 * Num2
        Loop Until Product < DiffMax And Num1 <> 1 And Num2 <> 1
 
        Return New Integer() {Product, Num1, Num2}
    End Function
End Class

Open in new window

0
 
ljhodgettAuthor Commented:
Hiya Paul,

Many thanks for that. It seems to work well but it still repeats itself a lot with the number 1 - 9 when the difficulty level is set to 9.

Best Regards
Lee
0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
PaulHewsCommented:
Difficulty = 9
There are only a few possible results when we exclude the 1* types:

9/3=3
8/2 =4
8/4=2
6/3 =2
6/2=3
4/2=2

It's not really enough to make a challenging problem, and it's too few to create 12 puzzles.  
0
 
ljhodgettAuthor Commented:
Ah right,

Sorry I did't relise that the 1's were excluded. Is it possible to add these in as it would still be good to have the likes of 7 / 1 etc and then at least it will be able to fill all 12.

Many thanks for your help.

Lee
0
 
ljhodgettAuthor Commented:
Many Thanks for all you help Paul

Cheers
Lee
0
All Courses

From novice to tech pro — start learning today.