网站首页 > 文章中心 > 其它

vb.net取随机值

作者:小编 更新时间:2023-10-07 11:03:18 浏览量:135人看过

vb.net取随机数

取随机数可以用 Random 类.

vb.net产生随机数

方法名称: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)

vb.net中产生三位正整数的随机数的程序写法

VB.NET中随机产生十个数字

添加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取随机值相关主题介绍,如果您觉得小编更新的文章只要能对粉丝们有用,就是我们最大的鼓励和动力,不要忘记讲本站分享给您身边的朋友哦!!

版权声明:倡导尊重与保护知识产权。未经许可,任何人不得复制、转载、或以其他方式使用本站《原创》内容,违者将追究其法律责任。本站文章内容,部分图片来源于网络,如有侵权,请联系我们修改或者删除处理。

编辑推荐

热门文章