1到50...
用RND取5個亂數
且5個數字不能重複
2006-01-22 09:03:08 · 4 個解答 · 發問者 Jimmy 1 in 電腦與網際網路 ➔ 程式設計
我用了最簡單的方法,洗牌唷!!
Private Sub Form_Activate()
Randomize Timer
Dim a(0 To 50) As Integer
For i = 1 To 50
a(i) = i
Next i
For i = 1 To 100
x = Int(Rnd * 50) + 1: y = Int(Rnd * 50) + 1
c = a(x): a(x) = a(y): a(y) = c
Next i
Print a(1); a(2); a(3); a(4); a(5)
End Sub
2006-01-22 15:36:25 · answer #1 · answered by 山下ㄉ魷魚 2 · 0⤊ 0⤋
表單上放置一個CommandButton
Private Sub Command1_Click()
Dim A(1 To 50) As Boolean, I As Integer, N As Integer
Cls
Randomize
For I = 1 To 5
Do
N = Int(Rnd * 50) + 1
Loop Until A(N) = False
A(N) = True
Print N;
Next
End Sub
2006-01-22 12:01:40 · answer #2 · answered by W.J.S. 7 · 1⤊ 0⤋
Private Sub Command1_Click()
Dim a() As Integer
Dim i As Integer, j As Integer, n As Integer
Dim rp As Boolean
Randomize Timer
Do
'產生新的數字
n = Int(Rnd() * 50) + 1
'檢查是否重覆
rp = False
For j = 1 To i
If a(j) = n Then
rp = True
Exit For
End If
Next
'沒有重覆就加入陣列中
If Not rp Then
i = i + 1
ReDim Preserve a(1 To i) As Integer
a(i) = n
End If
Loop Until i = 5
'顯示 5 個數字
For i = 1 To UBound(a)
Debug.Print a(i),
Next
Debug.Print
End Sub
2006-01-24 03:28:34 · answer #3 · answered by Sean 5 · 0⤊ 0⤋
拉一個Button
Private Sub Command1_Click()
Dim num%(5), i%, j%, chk As Boolean
Cls
For i = 1 To 5
chk = True
Do While chk
Randomize
num(i) = Int(Rnd * 50) + 1
chk = False
For j = 1 To i - 1
If num(i) = num(j) Then chk = True: Exit For
Next j
Loop
Print num(i);
Next i
End Sub
2006-01-22 09:47:04 · answer #4 · answered by ? 6 · 0⤊ 0⤋