vb随机数题(vb 随机数)
今天给各位分享vb随机数题的知识,其中也会对vb 随机数进行解释,如果能碰巧解决你现在面临的问题,别忘了关注本站,现在开始吧!
vb作业,随机数
Private Sub Command1_Click()
Dim a(25) As Integer, i As Integer, j As Integer
For i = 1 To 25
a(i) = Fix(Rnd * 95) + 5
Picture1.Print a(i); " ";
If i Mod 5 = 0 Then Picture1.Print
Next
For i = 1 To 25
If a(i) Mod 2 = 1 Then
Picture2.Print a(i);
j = j + 1
End If
Next
Picture2.Print
Picture2.Print j
For i = 1 To 25
For j = 1 To 25 - i
If a(j) a(j + 1) Then
a(0) = a(j)
a(j) = a(j + 1)
a(j + 1) = a(0)
End If
Next
Next
For i = 1 To 25
Picture3.Print a(i); " ";
If i Mod 5 = 0 Then Picture3.Print
Next
End Sub
VB如何生成随机数
1、打开“Microsoft Visual Basic 6.0 中文版”软件,准备一个空白的工程文件:
2、然后需要一个命令按钮,单击左侧工具箱中的“命令按钮”,在窗体中右侧拖动鼠标左键画出即可:
3、双击该命令按钮,这里演示输入代码打印0~10之间的任意随即整数,生成其他范围的随机整数公式为“int((上限-下限+1)*Rnd+下限)”,如果是1到53就是“int((1-53+1)*Rnd+53”:
4、写完之后运行程序,每单击一次命令按钮,就会在窗体上打印一个0~10之间的随即整数:
VB中,随机产生10个0~100之间的随机数,按从小到大的顺序排列怎么做?
可用冒泡排序法:
代码如下:
'不重复随机数排序
'添加 Command1 Command2 Command3Option Explicit Dim i, j, k, t, Trec, Arr(), RndVal, TmpVal, S Private Sub Form_Load()
Command1.Caption = "选随机数"
Command2.Caption = "小到大排序"
Command3.Caption = "大到小排序"
'*************************************** 安排控件摆放位置
Me.Move (Screen.Width - Me.Width) \ 2, (Screen.Height - Me.Height) \ 2
1 - 100 之间选 20 个随机数
Trec = 10 End Sub Private Sub Command1_Click() S = GetRnd(1, 100, Trec) Me.Cls
For i = 0 To Trec - 1 Print S(i) Next i End Sub Private Sub Command2_Click()
'********************** 由小排到大(冒泡排序法)
Me.Cls For i = 0 To Trec - 1 k = i For j = i + 1 To Trec - 1 If Arr(k) Arr(j) Then k = j
Next j t = Arr(i): Arr(i) = Arr(k): Arr(k) = t Print Arr(i) Next i
End Sub Private Sub Command3_Click()
'********************** 由大排到小(冒泡排序法)
Me.Cls For i = 0 To Trec - 1 k = i For j = i + 1 To Trec - 1 If Arr(k) Arr(j) Then k = j
Next j t = Arr(i): Arr(i) = Arr(k): Arr(k) = t Print Arr(i) Next i End Sub Public Function GetRnd(MinVal, MaxVal, Qty) As Long() Randomize For i = MinVal To MaxVal
ReDim Preserve Arr(i) Arr(i) = i Next For i = MinVal To MaxVal
RndVal = Int(Rnd * (MaxVal - MinVal + 1)) + MinVal TmpVal = Arr(i)= Arr(RndVal)
Arr(RndVal) = TmpVal Next For i = 0 To Qty - 1 Arr(i) = Arr(i + MinVal) Next GetRnd = Arr()
End Function
vb题,随机数的产生
rnd()是VB6.0中随机产生0-1之间浮点数的函数,若要在第一个文本框(text1)中产生(0,1)之间的随机数,则用
text1.text=rnd
若在文本框2(text2)中产生(0,10)之间的随机实数,则
text2.text=rnd*10
若在文本框3(text3)中产生[10,100]的随机整数,则
text3.text=int(rnd*(100-10+1))+10
vb随机数题的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于vb 随机数、vb随机数题的信息别忘了在本站进行查找喔。