如何在VB.net中无重复随机数组?

时间:2015-06-05 11:32:33

标签: vb.net

Dim r As New Random
Dim A(2) As String
A(0) = "A"
A(1) = "B"
A(2) = "C"
MsgBox(A(r.Next(0,3)) & "," & A(r.Next(0,3)) & "," & A(r.Next(0,3)))

我想在结果中看到以下行之一:

A,B,C
A,C,B
C,B,A
C,A,B
B,A,C
B,C,A

1 个答案:

答案 0 :(得分:2)

按随机顺序对数组进行排序,然后转到0,1,2。

Dim r As New Random
Dim A(2) As String
A(0) = "A"
A(1) = "B"
A(2) = "C"

A = A.OrderBy(Function() r.Next).ToArray()

MsgBox(A(0) & "," & A(1) & "," & A(2)