| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛

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
```

```Private Sub Command1_Click()

Dim i, j, n, t As Long

Dim KLB(Nums) As Long  '定义一个变量，标注已生成
Dim NUM(Nums) As Long  '''实际顺序

Dim s As String, 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
NUM(i) = 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
'''''''
Dim inx, k, tmp
For i = 1 To 8     '注数
inx = (i - 1) * 10 + 1     '每行起始索引
For j = inx To inx + 8
For k = j + 1 To inx + 9
If NUM(j) > NUM(k) Then
tmp = NUM(j)
NUM(j) = NUM(k)
NUM(k) = tmp
End If
Next k
Next j
Next i

For i = 1 To Nums
s = s & Format(NUM(i), "0#") & " "
Next i
''''''
Text1.Text = s        't = timeGetTime - t      'MsgBox t
End Sub

```

[此贴子已经被作者于2023-8-20 12:57编辑过]

Private Sub Command1_Click()

Dim i, j, n, t As Long

Dim KLB(Nums) As Long  '定义一个变量，标注已生成
Dim NUM(Nums) As Long  '''实际顺序

Dim s As String, 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
NUM(i) = 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
'''''''
Dim inx, k, tmp
For i = 1 To 8     '注数
inx = (i - 1) * 10 + 1     '每行起始索引
For j = inx To inx + 8
For k = j + 1 To inx + 9
If NUM(j) > NUM(k) Then
tmp = NUM(j)
NUM(j) = NUM(k)
NUM(k) = tmp
End If
Next k
Next j
Next i

For i = 1 To Nums
s = s & Format(NUM(i), "0#") & " "
Next i
''''''
Text1.Text = s        't = timeGetTime - t      'MsgBox t
End Sub

• 10
• 1/1页
• 1