作者icene (kkk)
看板Visual_Basic
标题Re: [VBA ] 如何用VBA产生1-81之间的乱数?
时间Wed Nov 30 23:09:26 2005
我想了另一种方法,不晓得合不合逻辑
Private Sub CommandButton1_Click()
For i = 1 To 81
Cells(i, 1) = Rnd()
Cells(i, 2) = i
Next i
Range("a1:b81").Sort Key1:=Range("A1"), Order1:=xlAscending
End Sub
'********************************
随机跑一排乱数,给他们1到81号码牌
再把那排乱数排序,1~81号号码牌也跟着乱掉
b栏就产生了1~81号不重复的乱数
*********************************
※ 引述《realll (IMOK)》之铭言:
: 我研究出来了!!!
: 分享给大家~!
: Sub imok() 'Private
: Dim a, i, j, z, x, y As Integer
: Dim AD As Double
: Dim renn(1, 90)
: Dim ren(1, 90) '伫列长度
: Set ws1 = Worksheets("Sheet1")
: Set ws2 = Worksheets("Sheet2")
: Set ws3 = Worksheets("Sheet3")
: Dim temp As Double
: Dim check As Integer
: Randomize Timer
: Dim N(80)
: For i = 0 To 79 '乱数序列中不会有相同的数字
: r = 1
: Do Until r <> 1 'r = 1 表示n(i)的乱数有重覆
: N(i) = Int(80 * Rnd) + 1
: r = 0
: For j = 0 To i - 1
: If N(i) = N(j) Then r = 1
: Next
: Loop
: ws1.Cells(i + 1, 4) = N(i)
: Next
: ws1.Cells(1, 4) = 81
: End Sub
--
※ 发信站: 批踢踢实业坊(ptt.cc)
◆ From: 219.91.83.128