有80个随机数,怎样只给固定位置的数排列?
有80个随机数,怎样只给固定位置的数排列?例如只给1-10的随机数排列,给11-20、21-30、31-40、41-50、51-60、61-70、71-80的随机数排列,示例见图片,源码见附件。
80个不重复的随机数.rar
(8.05 KB)
程序代码:
Option Explicit Option Base 1 Private Declare Function timeGetTime Lib "winmm.dll" () As Long Dim a(1 To 7) As Long Const Nums = 80 '定义一个常量80 Private Sub Command1_Click() Dim i, j, n, t As Long Dim KLB(Nums) As Long '定义一个变量 Dim s, m As String '定义字符串 t = timeGetTime Randomize For i = 1 To Nums 'i = 1 到 80 n = Int(Rnd * Nums) + 1 'n为随机数 If KLB(n) = 0 Then KLB(n) = n If n < 10 Then '判断是否是小于10 n = 0 & n '小于10则在前面加0 End If s = s & n & " " 's = s & n & vbCrLf Else i = i - 1 End If Next i Text1.Text = s 't = timeGetTime - t 'MsgBox t End Sub Private Sub Command2_Click() Dim b(10) As Integer '定义一个一维数组 Dim i, j As Integer Dim m As String For i = 1 To 10 b(i) = Int(Rnd * 90 + 10) '装入一批随机数 Print b(i); '打印输出,后面一个分号就不换行 Next i For i = 1 To 9 For j = i + 1 To 10 If b(i) > b(j) Then '第一个和第二个比较 m = b(i) b(i) = b(j) b(j) = m End If Next j, i Print For i = 1 To 10 Print b(i); '打印输出,后面一个分号就不换行 Next i End Sub Private Sub Command3_Click() Const Nums = 80 Dim KLB(Nums) As Long Dim i, j, l, h As Long Dim s, t, n As String '定义字符串 Randomize For i = 1 To Nums 'i = 1 到 80 n = Int(Rnd * Nums) + 1 'n为随机数 KLB(i) = n Next i s = "" For i = 1 To Nums s = s & KLB(i) & " " Next i 'Debug.Print s l = 10 h = 30 For i = l To h - 1 For j = i To h If KLB(i) > KLB(j) Then t = KLB(i) KLB(i) = KLB(j) KLB(j) = t End If Next j Next i s = "" For i = 1 To Nums s = s & KLB(i) & " " Next i ' Debug.Print s Text1.Text = s End Sub Private Sub Form_DblClick() Const Nums = 80 Dim KLB(Nums) As Long Dim i, j, n, l, h As Long Dim s, t As String '定义字符串 Randomize For i = 1 To Nums 'i = 1 到 80 n = Int(Rnd * Nums) + 1 'n为随机数 KLB(i) = n Next i s = "" For i = 1 To Nums s = s & KLB(i) & " " Next i Debug.Print s l = 10 h = 30 For i = l To h - 1 For j = i To h If KLB(i) > KLB(j) Then t = KLB(i) KLB(i) = KLB(j) KLB(j) = t End If Next j Next i s = "" For i = 1 To Nums s = s & KLB(i) & " " Next i Debug.Print s End Sub