2

I'm trying to loop this array 100 times (gets a number from 0-99,999). I've tried it several different ways with no luck. I've got it set up so you click a button and it generates the array. The user will then enter a number and hit the other button to check how many times that number shows up in the group of a 100 numbers. Any thoughts how to fix it? I'm stuck.

   Dim i As Integer
    Dim rnd As New Random()
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEnter.Click


            Dim num As Integer
            If Int32.TryParse(txtEnter.Text, num) AndAlso num >= 0 AndAlso num < 10 Then
            lblNumber.Text = i.ToString()
            Dim counts = From c In i.ToString()
                         Group By c Into Group
                         Select DigitGroup = New With {.Count = Group.Count(), .Digit = c, .Group = Group}
                         Order By DigitGroup.Count Descending, DigitGroup.Digit
                Dim numCount = counts.FirstOrDefault(Function(grp) grp.Digit.ToString() = num.ToString())
                If numCount IsNot Nothing Then
                    Dim numMessage = String.Format("There are {0} number {1}'s found.", numCount.Count, num)
                    MessageBox.Show(numMessage)
                Else
                    MessageBox.Show("Your number does not contain a " & num)
                End If
            Else
                MessageBox.Show("Please enter a number between 0 and 9.")
            End If

    End Sub

    Private Sub btnGetNumber_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnGetNumber.Click
        btnEnter.Enabled = True


        i = 0
        Do Until i > 100
            Dim randomInt = rnd.Next(0, 100000)
            i = i + 1
            i = rnd.Next(0, 100000)
        Loop
    End Sub
End Class
Jimmy
  • 21
  • 3

3 Answers3

2

Move the New Random() out of that loop. Make it global and create the instance just one time.

(The Random generator starts using a seed obtained from the current time. Declaring a Random variable inside a loop result in the same seed used again and again. This gives back the same number defeating the randomness of the function) It is like: http://xkcd.com/221/

See another explanation here

Of course, if you don't increment the variable i used to control the loop, you will loop forever

Dim i As Integer
Dim rnd As New Random()

.....

i = 0
Do Until i > 100
    Dim randomInt = rnd.Next(0, 100000)
    i = i + 1
Loop

Said that, now I can't get what are you trying to do here. There is no array to check and you do not use the value obtained from the Random generator, simply, your i variable exits with value 101.
Now I am just guessing what are your intentions, but perhaps you want just this?

i = rnd.Next(0, 100000)

This will give to the variable i a random number and your subsequent code checks how many, of the required digit, are present in i

Community
  • 1
  • 1
Steve
  • 213,761
  • 22
  • 232
  • 286
  • I edited my code.. it works but it only tests 1 randomly generated number from 0-99,999. I still need the program to test 99 other "numbers". Also, I should be using an array for this, I thought I was but I guess I don't really know what an array is? How can I do with with an array? Thanks for your help! – Jimmy Mar 28 '13 at 00:30
  • So you want to generate 100 random numbers and then check in each number how many digits (from 0-9) are present in that array of numbers? Example (23498, 98687 => there are 3 digits '8') – Steve Mar 28 '13 at 09:26
2

First of all, it is common to use For Loops when you know the number of loops.

Secondly, you forgot to increment i in the until loop

Finally, you don't need to redeclare rnd in every loop. Something like that sounds better to me :

Dim rnd As New Random()

For i = 1 To 100
    Dim randomInt = rnd.Next( 0 , 100000 )
Next
Giwrgos Tsopanoglou
  • 1,165
  • 2
  • 8
  • 15
1

I see that you are not adding the variable "i" to increment:

i = 1
Do Until i > 100
        Dim rnd As New Random()
        Dim randomInt = rnd.Next(0, 100000)
        i+=1 'Increment value of i by 1, same as i = i + 1 
Loop

The variable of "i" will always be 1 if not incremented. Check that and let me know if that solves the problem!

Exel Gamboa
  • 936
  • 1
  • 14
  • 25