取随机数可以用 Random 类.
方法名称:next(i);功能描述:返回一个0~i之间的整数
方法名称:next(i,j);功能描述:返回一个i~j之间的整数
方法名称:Nextdouble();功能描述:返回一个0~1之间的随机小数
================
使用方法:
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
Print a(i)
添加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
ListBox1.Items.Add(Str(N(I)))
Strarr = Strarr Str(N(I)) " "
'添加标签的横向数字序列
Label1.Text = Strarr
'获取最大值
Private Function GetMax() As Integer
Dim intMax As Integer
intMax = 0
intMax = IIf(intMax N(I), intMax, N(I))
Return intMax
End Function
'获取最小值
Private Function GetMin() As Integer
Dim intMin As Integer
intMin = 101
intMin = IIf(intMin N(I), intMin, N(I))
Return intMin
'获取平均值
Private Function GetAve() As Single
Dim Sum As Integer
Sum = 0
Sum = Sum + N(I)
Return Sum / 10
'生成随机数,显示各种值
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 Class
以上就是土嘎嘎小编为大家整理的vb.net取随机值相关主题介绍,如果您觉得小编更新的文章只要能对粉丝们有用,就是我们最大的鼓励和动力,不要忘记讲本站分享给您身边的朋友哦!!