VB生成随机数字
我想做个软件,需要把从1-20这20个数字,生成若干组,每组由7个不同的数字组成的数组.要求全部可能的组合都要生成.欢迎高手提供代码.感谢鼠标上的节奏的回答,貌似我没有解...
我想做个软件,需要把从1-20这20个数字,生成若干组,每组由7个不同的数字组成的数组.要求全部可能的组合都要生成.欢迎高手提供代码.
感谢鼠标上的节奏的回答,貌似我没有解释清楚,是要把这20个数字,全部生成每组为7个数字的组合,并且,每个组合要从小到大排序.关键是生成所有的组合,并且要从小到大排序.我提高悬赏,欢迎再给我想下吧. 展开
感谢鼠标上的节奏的回答,貌似我没有解释清楚,是要把这20个数字,全部生成每组为7个数字的组合,并且,每个组合要从小到大排序.关键是生成所有的组合,并且要从小到大排序.我提高悬赏,欢迎再给我想下吧. 展开
3个回答
展开全部
相信这是你要的结果,随机数范围是1-20,可以设置,一组返回7个数字,也可以设置,最后给你加了个升序排序下去,可以的话就再高点分吧。(*^__^*)
Option Base 1 '定义数组下标从1开始
Private Sub Command1_Click()
Dim i As Integer, j As Integer
Dim num_array As String
Dim str As String
For i = 1 To 5 '输出5组
num_array = Rand_Num(20, 7)
str = "第" & i & "组:" & num_array
Print str
Next
End Sub
Function Rand_Num(max As Integer, count As Integer) As String
'max为随机数的最大范围,count为生成一组的数字个数
Dim i As Integer, j As Integer, k As Integer, t As Integer, rand As Integer
ReDim Sum(max) As Integer
Dim str As String
Dim min As Integer
Randomize
If max < count Then max = count
For i = 1 To max '赋值到数组sum
Sum(i) = i
Next
For j = max To 2 Step -1 '打乱sum数组里的数字排序
rand = Int(Rnd * (j - 1)) + 1
t = Sum(j): Sum(j) = Sum(rand): Sum(rand) = t
Next
min = Sum(1)
For i = 1 To count - 1 '以该例子为例,取前七个数进行升序排序
For j = i + 1 To count
If Sum(i) > Sum(j) Then t = Sum(i): Sum(i) = Sum(j): Sum(j) = t
Next
Next
For k = 1 To count '把排序后的结果取出组合返回
str = str & Sum(k) & " "
Next
Rand_Num = str
End Function
Option Base 1 '定义数组下标从1开始
Private Sub Command1_Click()
Dim i As Integer, j As Integer
Dim num_array As String
Dim str As String
For i = 1 To 5 '输出5组
num_array = Rand_Num(20, 7)
str = "第" & i & "组:" & num_array
Print str
Next
End Sub
Function Rand_Num(max As Integer, count As Integer) As String
'max为随机数的最大范围,count为生成一组的数字个数
Dim i As Integer, j As Integer, k As Integer, t As Integer, rand As Integer
ReDim Sum(max) As Integer
Dim str As String
Dim min As Integer
Randomize
If max < count Then max = count
For i = 1 To max '赋值到数组sum
Sum(i) = i
Next
For j = max To 2 Step -1 '打乱sum数组里的数字排序
rand = Int(Rnd * (j - 1)) + 1
t = Sum(j): Sum(j) = Sum(rand): Sum(rand) = t
Next
min = Sum(1)
For i = 1 To count - 1 '以该例子为例,取前七个数进行升序排序
For j = i + 1 To count
If Sum(i) > Sum(j) Then t = Sum(i): Sum(i) = Sum(j): Sum(j) = t
Next
Next
For k = 1 To count '把排序后的结果取出组合返回
str = str & Sum(k) & " "
Next
Rand_Num = str
End Function
展开全部
没有调试过,试试这段代码:
添加一个label1
dim a1 as Integer,a2 as Integer,a3 as Integer,a4 as Integer,a5 as Integer,a6 as Integer,a7 as Integer
dim mystr as string
for a1=1 to 14
for a2=a1+1 to 15
for a3=a2+1 to 16
for a4=a3+1 to 17
for a5=a4+1 to 18
for a6=a5+1 to 19
for a7=a6+1 to 20
doevent
mystr=format(a1,"00 ") & format(a2,"00 ") & format(a3,"00 ") & format(a4,"00 ") & format(a5,"00 ") & format(a6,"00 ") & format(a7,"00 ")
label1.caption= mystr
next
next
next
next
next
next
next
添加一个label1
dim a1 as Integer,a2 as Integer,a3 as Integer,a4 as Integer,a5 as Integer,a6 as Integer,a7 as Integer
dim mystr as string
for a1=1 to 14
for a2=a1+1 to 15
for a3=a2+1 to 16
for a4=a3+1 to 17
for a5=a4+1 to 18
for a6=a5+1 to 19
for a7=a6+1 to 20
doevent
mystr=format(a1,"00 ") & format(a2,"00 ") & format(a3,"00 ") & format(a4,"00 ") & format(a5,"00 ") & format(a6,"00 ") & format(a7,"00 ")
label1.caption= mystr
next
next
next
next
next
next
next
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
哎,我调试都死掉了,long组的数据
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询