Private Sub Command1_Click()
创新互联建站是专业的温泉网站建设公司,温泉接单;提供成都网站设计、成都做网站,网页设计,网站设计,建网站,PHP网站建设等专业做网站服务;采用PHP框架,可快速的进行温泉网站开发网页制作和功能扩展;专业做搜索引擎喜爱的网站,专业的做网站团队,希望更多企业前来合作!
Dim a(1 To 100) As Integer
Dim i As Integer, j As Integer, k As Integer
For i = 1 To 100 '给数组a一百个元素赋值,并换每行十个数字输出来窗体上
a(i) = Int(Rnd * 101)
k = k + 1
Print Tab((k - 1) * 5); a(i);
If k = 10 Then k = 0: Print
Next i
For i = 100 To 2 Step -1 '用冒泡排序法对数组进行排序
For j = 1 To i - 1
If a(j) a(j + 1) Then
t = a(j): a(j) = a(j + 1): a(j + 1) = t
End If
Next j
Next i
For i = 1 To 100 '输出排好序的数组
k = k + 1
Print Tab((k - 1) * 5); a(i);
If k = 10 Then k = 0: Print
Next i
End Sub
可以用Rnd函数实现
Rnd 函数返回小于 1 但大于或等于 0 的值。
为了生成某个范围内的随机整数,可使用以下公式:
Int((upperbound - lowerbound + 1) * Rnd + lowerbound)
这里,upperbound 是随机数范围的上限,而 lowerbound 则是随机数范围的下限。
你这里可以先把范围扩大10倍,看成21到34之间的随机数,最后再除以10就行了。
Int((34 - 21 + 1) * Rnd + 21) / 10
'项目:VB中产生在一定正整数范围内的不重复的随机数'作者:qq15495835Private Sub Command1_Click() Max = 100 '随机数上限值 Min = 1 '随机数下限值 Amount = 10 '产生号码数量(数量值应小于随机数上限值-随机数下限值)否则会产生死循环 ReDim a(Amount) Randomize For i = 0 To Amount a(i) = Int((Max - Min + 1) * Rnd + Min) For j = 0 To i If i j And a(i) = a(j) Then i = i - 1 Next Next Text1 = Join(a, vbCrLf)End Sub'请采纳参考!谢谢!
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Randomize() '产生随机数种子 以防止每次生成结果都一样
Dim a(100) As Integer '声明数组存放数据 用来保存随机数结果
Dim max As Integer, min As Integer '声明两个变量存最大值和最小值
min = 1000 : max = 0 '设置初值
For i = 0 To 99 '循环100次
a(i) = Int(Rnd() * 1000 + 1) '用rnd生成一个随机数 由于rnd范围为0-1之间的小数.所以*1000设置他的范围为0到999之间 加1变成1到1000之间
If a(i) = max Then max = a(i) '如果当前数值大于最大值的变量就保存
If a(i) = min Then min = a(i) '如果当前数值小于最小值的变量就保存
Next
MsgBox("最大值为" max)
MsgBox("最小值为" min)
End Sub
Private Sub Command1_Click()
Dim num(6) As Integer
Dim i As Integer, j As Integer
Dim tmp As Integer
Randomize '防止每次生出随机数一样
For i = 0 To 5
num(i) = Int(Rnd * 53) + 1
Next
PrintNum "新生成的6个随机数为:", num()
For i = 0 To 5
For j = 0 To 5
If num(j) num(j + 1) Then
tmp = num(j)
num(j) = num(j + 1)
num(j + 1) = tmp
End If
Next
Next
PrintNum "排序后的6个随机数为:", num()
End Sub