vb.net产生随机数组 vb随机产生一个数

VB,NET中为什么每次产生的第一次产生随机数都相同因为是伪随机数?。?你要给个随机种子
Dim rnd As Random = New Random(System.DateTime.Now.Millisecond) //通过日期产生随机数
VB 数组随机排列方法很多,在窗体上添加一个按钮,两个标签,见下图:
窗体的AutoRedraw 属性修改为True,两个标签的AutoSize 属性修都改为True 。
启动后见下图:
单击按钮先下图:
完整代码如下:
Option Explicit
Private d(7, 5) As Integer
Private a(35) As Integer
Private Sub Command1_Click()
Dim i As Integer
Dim j As Integer
Dim n As Integer
'生成1-35随机序列存入数组A
n = 0
Randomize
Do While n35
j = Int(Rnd * 351)
For i = 1 To n
If j = a(i) Then Exit For
Next i
If in Then
n = n1
a(n) = j
End If
Loop
'按照数组A系列重新给数组D赋值
n = 0
For i = 1 To 7
For j = 1 To 5
n = n1
d(i, j) = a(n)
Next j
Next i
'在标签2里显示
Label2.Caption = ""vbCrLf
For i = 1 To 7
For j = 1 To 5
Label2.Caption = Label2.CaptionRight(""CStr(d(i, j)), 4)
Next j
Label2.Caption = Label2.CaptionvbCrLf
Next i
End Sub
Private Sub Form_Load()
Dim i As Integer
Dim j As Integer
Dim n As Integer
'数组D初始化
n = 0
For i = 1 To 7
For j = 1 To 5
n = n1
d(i, j) = n
Next j
Next i
'在标签1里显示
Label1.Caption = ""vbCrLf
For i = 1 To 7
For j = 1 To 5
Label1.Caption = Label1.CaptionRight(""CStr(d(i, j)), 4)
Next j
Label1.Caption = Label1.CaptionvbCrLf
Next i
End Sub
vb.net代码声明一个一维字符类型的数组,有20个元素,每个元素最多存放十个字符 。字符个数随机,Dim a(20) As String
Dim num As Integer
Dim ppp As String = ""
Dim temp As String = ""
For i = 1 To 20
temp = ""
num = Int(Rnd() * 101)
For j = 1 To num
temp = tempChr(Int(Rnd() * 2697))
Next
a(i) = temp
Next
Dim max As Integer = 0
For i = 1 To 20
If a(i).Lengthmax Then
max = a(i).Length
End If
Next
For i = 1 To 20
If a(i).Length = max Then
ppp = ppp"字符最多的元素是第"i"个元素"a(i)vbCrLf
End If
Next
For i = 1 To 20
TextBox1.Text = TextBox1.Texta(i).PadRight(10)""
If i Mod 4 = 0 Then
TextBox1.Text = TextBox1.TextvbCrLf
End If
Next
TextBox1.Text = TextBox1.Textppp
vb.net的随机3个数字,然后排序一下给你一个最简单的冒泡排序代码:
将三个数放到一个数组中 。
a(0)=val(text1.text):a(1)=val(text2.text):a(2)=val(text3.text)
dim flag as Boolean,temp as Integer
for i = 0 to 2
flag = true
for j = 2 to 1
if a(j)a(j-1) then
temp = a(j-1)
a(j-1) = a(j)
a(j) = temp
flag = false
end if
next j
if flag then Exit For
next i
text4.text=a(0):text5.text=a(1):text6.text=a(2)
vb随机取数组内数字 , 不重复!Private Sub Command1_Click()
MsgBox GetRndItem(3)
End Sub
Function GetRndItem(num As Integer) As String
Dim a
a = Split("125 625#235 456#658 125#125 151#152 841#152 151", "#")
Dim i As Integer
Randomize
Dim Index As Integer
Dim Text As String
Dim arU As Integer
arU = UBound(a)
If numarU1 Then num = arU1
For i = 1 To num
Index = Int(Rnd * arU)
Text = TextvbTaba(Index)
a(Index) = a(arU)
arU = arU - 1
Next
GetRndItem = Mid(Text, 2)
End Function
每次产生一随机数组索引,随机索引范围是从最小下标到最大下标前一位 。取出随机位的数组元素
之后,用最后一数组元素替换该位,然后把最后一位用Redim preserve压缩掉 。不过压缩不用了,只要用一个变量arU来表示数组中最大有效数下标位就可 。
【vb.net产生随机数组 vb随机产生一个数】关于vb.net产生随机数组和vb随机产生一个数的介绍到此就结束了 , 不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息 , 记得收藏关注本站 。

    推荐阅读