2011-07-12 102 views
0

我想從數字號碼中隨機 列表的列表中選擇一些uniq的數字:從列表中隨機挑選

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

我想從這個名單

[2,6,16] 
+0

的可能重複[C#:是使用隨機和排序依據良好的洗牌算法(http://stackoverflow.com/questions/1287567/c-is-using-random-and-orderby -a-good-shuffle-algorithm) –

+2

(我所鏈接的問題實際上並不是最接近的重複*問題*,但它有最相關的答案* IMO :) –

+0

@Jon:我猜對了誰那個答案的作者是。 :) – John

回答

0
隨機挑選一些uniq的數字

是否這樣?

 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) 
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