使用EXCEL 生成不重复的随机数的VBA程序
Option Base 1
Const M As Integer = 100
Const N As Integer = 60
Private Sub CommandButton1_Click()
Dim i As Integer, a(M) As Integer, x As Integer, arr(1 To N)
'从100个数中选择60个数,这60个数不能重复,
Do While i < N
x = Int(Rnd * (M - 1) + 1)
If a(x) = 0 Then
a(x) = 1
i = i + 1
arr(i) = x '结果放到数组当中待用
End If
Loop
For j = 1 To M
Cells(j, 1) = 0
Cells(j, 2) = Round(Rnd * (3# - 2#) + 2#, 2)
Cells(j, 3) = Round(Rnd * (0.2 - 0.13) + 0.13, 2)
Cells(j, 4) = Round(Rnd * (0.7 - 0.5) + 0.5, 2)
Next j
For l = 1 To N
Cells(arr(l), 1).Value = 1 '60个数组的位置赋值为1
Next l
End Sub