谁能帮我写一段随机抽取数的代码?

yaozheng 2003-09-23 08:05:08
很简单的,就是从1到20中,随机抽取10个互不相同的数出来,就像一些彩票软件那样。我冥思苦想一夜不得要领。请大家帮忙,多谢了!
...全文
95 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
northwolves 2003-09-24
  • 打赏
  • 举报
回复
用集合要简单一些:

Private Sub Form_Load()
Dim temp As New Collection
Dim a(9) As String, num As Integer
For i = 1 To 20
temp.Add i
Next
Randomize
For i = 0 To 9
num = Int(Rnd * temp.Count + 1)
a(i) = temp(num)
temp.Remove num
Next
Set temp = Nothing
MsgBox Join(a(), ","), 64, "10 random number between 1 to 20"
End Sub
crazybeatcode 2003-09-24
  • 打赏
  • 举报
回复
闪!不要看完我的代码再来屁扯
Fengq 2003-09-23
  • 打赏
  • 举报
回复
Dim a(10)
For i = 1 To 10
xx:
Randomize
a(i) = Int(Rnd() * 20 + 1)
For j = 1 To i - 1
If a(j) = a(i) Then GoTo xx
Next
Debug.Print a(i)

Next
crazybeatcode 2003-09-23
  • 打赏
  • 举报
回复
忘了:
set a=nothing
crazybeatcode 2003-09-23
  • 打赏
  • 举报
回复
dim a as new collection
dim i as integer
dim b as integer
for i = 1 to 20
a.add i
next
for i=1 to 10
randomize
b=int(rnd()*a.count)+1
debug.print a(b)
a.delete b
next
TechnoFantasy 2003-09-23
  • 打赏
  • 举报
回复
科班做法 :-)

Option Explicit
Option Base 0

Private Sub Command1_Click()
Dim a(19)
Dim i As Integer
Dim x As Integer
Dim j As Integer

For i = 0 To 19
a(i) = i
Next i

For i = 19 To 10 Step -1
Randomize
x = Int((i + 1) * Rnd)
Debug.Print a(x) + 1,
For j = x To i - 1
a(j) = a(j + 1)
Next j
Next i

Debug.Print
End Sub
vansoft 2003-09-23
  • 打赏
  • 举报
回复
TechnoFantasy(www.applevb.com)
这位仁兄的数组为什么空个a(20)不用啊?
我想一定不是VB科班出身的。

程序基本上是对的,取10至19不重复的数。
TechnoFantasy 2003-09-23
  • 打赏
  • 举报
回复
Debug里面输出的就是随即生成的数字
TechnoFantasy 2003-09-23
  • 打赏
  • 举报
回复
Private Sub Command1_Click()
Dim a(20)
Dim i As Integer
Dim x As Integer
Dim j As Integer

For i = 0 To 19
a(i) = i
Next i

For i = 19 To 10 Step -1
Randomize
x = Int((i + 1) * Rnd)
Debug.Print a(x) + 1,
For j = x To i - 1
a(j) = a(j + 1)
Next j
Next i

Debug.Print
End Sub

7,759

社区成员

发帖
与我相关
我的任务
社区描述
VB 基础类
社区管理员
  • VB基础类社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧