Visual Basic-随机数

时间:2017-10-30 16:38:25

标签: vb.net random

我正在尝试从1-99生成5个随机数,并将它们显示在ListBox中。谁能告诉我哪里出错了?现在我的代码显示ListBox中的所有99个数字,但我只想要显示其中的5个。这是代码:

'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

2 个答案:

答案 0 :(得分:1)

替换

For count As Integer = 0 To numbers.Count - 1

使用

For count As Integer = 1 To 5

答案 1 :(得分:0)

以上方法可行,但您需要在下一个语句之后添加计数。我建议您也要了解有关循环的更多信息。显然,Visual Basic 2012非常适合。