lundi 30 octobre 2017

Visual Basic- Random Numbers

I'm trying to generate 5 random numbers from 1-99 and display them in a ListBox. Can someone tell me where I'm going wrong? Right now my code is displaying all 99 numbers in the ListBox, but I only want 5 of them to display. Here is the code:

'list to store numbers
        Dim numbers As New List(Of Integer)

        'add desired numbers to list
        For count As Integer = 1 To 99
            numbers.Add(count)
        Next


        Dim Rnd As New Random
        Dim SB As New System.Text.StringBuilder
        Dim Temp As Integer

        'select a random number from the list, add to listbox and remove it so it can't be selected again
        For count As Integer = 0 To numbers.Count - 1
            Temp = Rnd.Next(0, numbers.Count)
            SB.Append(numbers(Temp) & "  ")
            ListBox2.Items.Add(numbers(Temp))
            numbers.RemoveAt(Temp)
        Next




Aucun commentaire:

Enregistrer un commentaire