vb怎么产生随机数? 指定随机数从text1至text2,点击按钮输出的3个随机数分别在3个标签中。
3个回答
展开全部
因为不知道你要的是整数还是小数我就先当整数做
private sub command1_click()
dim a as integer
dim b as integer
dim r(1 to 3) as integer
dim i as integer
a=val(text1.text)
b=val(text2.text)
for i=1 to 3
randomize
if a<b then
r(i)=int(rnd*(b-a+1)+a)
else
r(i)=int(rnd*(a-b+1)+b)
endif
next
label1.caption=str(a(1))
label2.caption=str(a(2))
label3.caption=str(a(3))
end sub
private sub command1_click()
dim a as integer
dim b as integer
dim r(1 to 3) as integer
dim i as integer
a=val(text1.text)
b=val(text2.text)
for i=1 to 3
randomize
if a<b then
r(i)=int(rnd*(b-a+1)+a)
else
r(i)=int(rnd*(a-b+1)+b)
endif
next
label1.caption=str(a(1))
label2.caption=str(a(2))
label3.caption=str(a(3))
end sub
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
text1和text2是否数值或太大之类的错误请自己添加,随便写了一个:
Private Sub Command1_Click()
Dim aa As String
Dim i, j, bb, cc As Long
If Val(Text1) > Val(Text2) Then
cc = Val(Text1) - Val(Text2)
Else
cc = Val(Text2) - Val(Text1)
End If
If cc > 10 Then
bb = 1
For j = 1 To Len(Format(cc, "0"))
bb = bb * 10
Next
End If
Randomize Timer
i = Int(Rnd() * bb) Mod cc
If Val(Text1) > Val(Text2) Then
i = i + Val(Text2)
Else
i = i + Val(Text1)
End If
Label1 = i
Randomize Timer
i = Int(Rnd() * bb) Mod cc
If Val(Text1) > Val(Text2) Then
i = i + Val(Text2)
Else
i = i + Val(Text1)
End If
Label2 = i
Randomize Timer
i = Int(Rnd() * bb) Mod cc
If Val(Text1) > Val(Text2) Then
i = i + Val(Text2)
Else
i = i + Val(Text1)
End If
Label3 = i
End Sub
Private Sub Command1_Click()
Dim aa As String
Dim i, j, bb, cc As Long
If Val(Text1) > Val(Text2) Then
cc = Val(Text1) - Val(Text2)
Else
cc = Val(Text2) - Val(Text1)
End If
If cc > 10 Then
bb = 1
For j = 1 To Len(Format(cc, "0"))
bb = bb * 10
Next
End If
Randomize Timer
i = Int(Rnd() * bb) Mod cc
If Val(Text1) > Val(Text2) Then
i = i + Val(Text2)
Else
i = i + Val(Text1)
End If
Label1 = i
Randomize Timer
i = Int(Rnd() * bb) Mod cc
If Val(Text1) > Val(Text2) Then
i = i + Val(Text2)
Else
i = i + Val(Text1)
End If
Label2 = i
Randomize Timer
i = Int(Rnd() * bb) Mod cc
If Val(Text1) > Val(Text2) Then
i = i + Val(Text2)
Else
i = i + Val(Text1)
End If
Label3 = i
End Sub
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
msgbox rnd(text1+(text1*text2-1))
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询