應該要怎麼改比較好~
要用亂數跑1-81個值並且不能重複跑出
要怎麼改才正確呢
Dim i, a As Integer
Private Sub Command1_Click(Index As Integer)
For i = 0 To 80
Command1(i).Caption = a
a = Int((Rnd * 81) + 1)
Next
End Sub
2006-05-17 07:22:27 · 4 個解答 · 發問者 ★闇黑〞攸月╣ 1 in 電腦與網際網路 ➔ 程式設計
Dim i, a As IntegerPrivate Sub Command1_Click(Index As Integer)Dim B(1 To 81) As BooleanFor i = 0 To 80 Do a = Int((Rnd * 81) + 1) Loop Until B(a) = False B(a) = True Command1(i).Caption = aNextEnd Sub
2006-05-17 08:31:23 · answer #1 · answered by W.J.S. 7 · 0⤊ 0⤋
要怎麼寫這個程式
請問大大能交我嗎?!
拜託 要做功課
2012-07-28 10:23:22 · answer #2 · answered by 詹姆士 1 · 0⤊ 0⤋
最佳答案應該是puppy 才對喔...
程式速度 你用用看就知道
應該差很多
2008-02-23 08:25:19 · answer #3 · answered by 我不知道 1 · 0⤊ 0⤋
重點在於不能重複跑出,直接產生81個亂數值而又不重複的機率太小,如果每產生一個亂數值,就要與已產生的亂數值做比對效率太低.
所以直接將1~81放入陣列a中,而後隨機產生亂數num1(0~80),然後將a(0)與a(num1)的內容做交換,第二次產生亂數num1後將a(1)與a(num1)內容做交換......一直到a(80)與a(num1)交換,如此就能有效率的產生不重複的81個亂數值.
程式如下,請在表單內放入一個CommandButton與一個TextBox.若你覺得只做一輪交換還不夠,你可以再外加一個迴圈.....
==================================================
Dim a(80) As Integer
Private Sub Command1_Click()
Randomize
Text1.Text = ""
For i = 0 To 80
num1 = Int(Rnd * 81) '隨機產生一亂數num1(0~80)
temp = a(i) '將a(i)與a(num1)之值做交換
a(i) = a(num1) '如此便不會產生重複現象
a(num1) = temp
Next i
For i = 0 To 80
Text1.Text = Text1.Text & a(i) & vbCrLf
Next i
End Sub
Private Sub Form_Load()
For i = 0 To 80 '初值化陣列a
a(i) = i + 1 'a(0)~a(80)依序擺入1~81
Next i
End Sub
2006-05-17 18:56:20 · answer #4 · answered by puppy 5 · 0⤊ 0⤋