ljhodgett
asked on
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_Leve l)
'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
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_Leve
'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
ASKER
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
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
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.
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.
ASKER
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
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
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Many Thanks for all you help Paul
Cheers
Lee
Cheers
Lee
Open in new window