Need a little VB.Net help. TheFinder was attempting to find a good solution to a lottery number picking problem. He needed to pick 6 numbers, each from 1 to 50 but no two duplicating. The six numbers had to total to a target value input by the user between 21 and 285.
I developed an algorithm (in GetSix below) that I believe will give a quick and simple solution to the problem. Unfortunately, I’m not VB.Net adept and I don’t have VB. If someone would like to fix up this code and give us a little idea of the performance (time and/or number of loops), that would be interesting.
Dim targetValue as Integer
Dim Num1, Num2, Num3, Num4, Num5, Num6 as Integer
Dim CountOfTries as Integer targetValue = 98 // targetValue would be user input between 21 and 285
If GetSix(targetValue, Num1, Num2, Num3, Num4, Num5, Num6, CountOfTries)
// here we’ve got 6 good values and the CountOfTries
// here we’ve got an error
Public Function GetSix(ByVal target As Integer ByRef N1 As Integer, ByRef N2 As Integer, ByRef N3 As Integer, ByRef N4 As Integer, ByRef N5 As Integer, ByRef N6 As Integer, ByRef CountLoop As Integer) as Boolean
Dim Success as Boolean = true
CountLoop = 0 Do until ((N1<>N2 and N1<>N3 and N1<>N4 and N1<>N5 and N1<>N6 and N2<>N3 and N2<>N4 and N2<>N5 and N2<>N6 and N3<>N4 and N3<>N5 and N3<>N6 and N4<>N5 and N4<>N6 and N5<>N6) or Success = False) CountLoop += 1 // CountLoop saves us from an endless loop – but that shouldn’t happen