YU 發問時間： 電腦與網際網路程式設計 · 5 年前

# vb怎樣第二輪抽號

http://i.imgur.com/XWGxUeg.png

### 3 個解答

• John
Lv 7
5 年前
最佳解答

Public Module Program

Public Sub Main(args() As string)

Dim numbers() As Integer = BuildNumber(100)

Shuffle(numbers)

Print(numbers)

Dim first50() As Integer = GetFirst(50, numbers)

Shuffle(first50)

Print(first50)

Dim first10() As Integer = GetFirst(10, first50)

Shuffle(first10)

Print(first10)

End Sub

Private Function BuildNumber(ByVal size As Integer) As Integer()

Dim results(size) As Integer

For i As Integer = 0 To size - 1

results(i) = i + 1

Next

Return results

End Function

Private Function GetFirst(ByVal size As Integer, ByVal numbers() As Integer) As Integer()

Dim results(size) As Integer

Array.Copy(numbers, results, size)

Return results

End Function

Private Sub Shuffle(ByRef numbers() As Integer)

Dim rnd As New Random

Dim pos As Integer, tmp As Integer

Dim size As Integer = numbers.Length - 1

For i As Integer = 0 To size

pos = rnd.Next(0, size)

tmp = numbers(i)

numbers(i) = numbers(pos)

numbers(pos) = tmp

Next

End Sub

Private Sub Print(ByVal numbers() As Integer)

For Each i As Integer In numbers

Console.Write("{0} ", i)

Next

Console.WriteLine()

End Sub

End Module

• 5 年前

Dim a(100) As Integer

Dim r(100) As Integer

Dim i As Integer

Dim j As Integer

Randomize()

For i = 1 To TextBox1.Text

a(i) = i

Next i

For i = TextBox1.Text To 1 Step -1

j = Int(Rnd() * i) + 1

r(i) = a(j)

a(j) = a(i)

Next i

For i = 1 To TextBox1.Text

TextBox1.Text &= r(i) & vbCrLf

Next i

For i = 1 To TextBox6.Text

myArray(i) = r(i)

TextBox2.Text &= myArray(i) & vbCrLf

Next

Dim r(50) As Integer

Dim i As Integer

Dim j As Integer

For i = 1 To 50

a(i) = i

Next i

For i = 50 To 1 Step -1

j = Int(Rnd() * i) + 1

r(i) = a(j)

a(j) = a(i)

Next i

For i = 1 To 10

myA(i) = myArray(r(i))

TextBox3.Text &= myA(i) & vbCrLf

Next i

• XiDeww
Lv 4
5 年前

做一個大小為50的陣列，把50個亂數存進去陣列，

之後以索引值作為亂數，假設陣列大小為1~50，那就取10個不重複1~50的亂數，

之後將取出來的10個亂數，找到陣列索引值，將其值輸出。

2015-07-01 20:00:31 補充：

意思是你在取一個亂數範圍1~50，而這個亂數就陣列的索引值，取出十個不重複。

假設我現在要先在1~10裡面抽出五個，再從五個號碼抽出三個。

先設一陣列

a[5] = {5,2,8,7,4}

之後再去取三個亂數

假設陣列的起始索引值是0，那最後一個索引值肯定是4這樣剛好5個，

那我就取0~4的亂數，取三個且不重複，假設取到0,1,4

那就去找a[0]，a[1]，a[4]