从列表中随机挑选

时间:2011-07-12 16:46:11

标签: .net vb.net list random

我想从一个数字列表中随机选择一些唯一的数字 数字列表:

dim firstlist() = [1,2,3,6,7,9,12,16]

我想从此列表中随机选择一些唯一数字

[2,6,16]

2 个答案:

答案 0 :(得分:0)

像这样?

        Dim firstlist() = {1, 2, 3, 6, 7, 9, 12, 16}
    Dim seclist As New Collection
    Dim rnd As New Random
    Debug.WriteLine("__")
    Dim i As Integer = 1
    While i <= 3
        Dim j As Integer = CInt(rnd.Next(0, firstlist.Length - 1))
        If Not seclist.Contains(j) Then
            seclist.Add(firstlist(j), j)
            Debug.WriteLine(CStr(firstlist(j)))
            i += 1
        End If
    End While
    'seclist now contains 3 values (data) and the indices (key)

答案 1 :(得分:0)

Dim firstlist = {1, 2, 3, 6, 7, 9, 12, 16}  
dim secondlist as new list(of integer)  
dim rand as new random()  
while not secondlist.count = 3  
    secondlist.add(firstlist(rand.next(firstlist.count-1)))  
end while