扫二维码与项目经理沟通
我们在微信上24小时期待你的声音
解答本文疑问/技术咨询/运营咨询/技术建议/互联网交流
取随机数可以用 Random 类。
成都创新互联是一家专业提供开阳企业网站建设,专注与成都网站建设、网站设计、H5场景定制、小程序制作等业务。10年已为开阳众多企业、政府机构等服务。创新互联专业网站制作公司优惠进行中。
给定10个数的话,可以把这10个数放在一个数组里,然后取0~9的范围内的随机数,用它当数组下标取数组成员即可。
方法名称:next();功能描述:返回一个0~2147483647之间的整数
方法名称:next(i);功能描述:返回一个0~i之间的整数
方法名称:next(i,j);功能描述:返回一个i~j之间的整数
方法名称:Nextdouble();功能描述:返回一个0~1之间的随机小数
================
使用方法:
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Dim rand = New System.Random()
MsgBox(rand.next().ToString())
End Sub
==========================
产生N个不同数的话,我给你个我写的VB的代码,你稍微改下:
产生0-10的数:
Private Sub Command1_Click()
Randomize
Dim a(10) As Long
n = 10
For i = 0 To n
a(i) = Int(Rnd() * (n + 1))
For j = 0 To i - 1
If a(i) = a(j) Then
i = i - 1
Exit For
End If
Next
Next
For i = 0 To n
Print a(i)
Next
End Sub
'项目: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'请采纳参考!谢谢!
添加listbox1,label1,command1
Public Class Form1
Dim N(10) As Integer
'生成随机数
Private Sub CreatRand()
Dim I As Integer
Dim Strarr As String
Strarr = ""
ListBox1.Items.Clear()
Dim R As New System.Random
For I = 0 To 9
N(I) = R.Next(30, 100)
ListBox1.Items.Add(Str(N(I)))
Strarr = Strarr Str(N(I)) " "
Next
'添加标签的横向数字序列
Label1.Text = Strarr
End Sub
'获取最大值
Private Function GetMax() As Integer
Dim I As Integer
Dim intMax As Integer
intMax = 0
For I = 0 To 9
intMax = IIf(intMax N(I), intMax, N(I))
Next
Return intMax
End Function
'获取最小值
Private Function GetMin() As Integer
Dim I As Integer
Dim intMin As Integer
intMin = 101
For I = 0 To 9
intMin = IIf(intMin N(I), intMin, N(I))
Next
Return intMin
End Function
'获取平均值
Private Function GetAve() As Single
Dim I As Integer
Dim Sum As Integer
Sum = 0
For I = 0 To 9
Sum = Sum + N(I)
Next
Return Sum / 10
End Function
'生成随机数,显示各种值
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Call CreatRand()
MsgBox("最大值=" Str(GetMax()) ",最小值=" Str(GetMin()) ",平均值=" Str(GetAve()))
End Sub
End Class
我们在微信上24小时期待你的声音
解答本文疑问/技术咨询/运营咨询/技术建议/互联网交流