产生随即数问题,小妹请教各位大虾!!!!

nslhao 2003-03-21 12:10:55
产生52个随机数,不能重复,请教算法??不胜感激。
...全文
57 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
nik_Amis 2003-03-21
  • 打赏
  • 举报
回复
手误,呵呵

for n=1 to 80
a=int(rnd*51):b=int(rnd*51)
s=y(a):y(a)=y(b):y(b)=s
next
考虑改为
for n=0 to 50
a=int(rnd*51):s=y(n):y(n)=y(a):y(a)=s
next
这样会好一点?讨论讨论
Cooly 2003-03-21
  • 打赏
  • 举报
回复
:) nik的算法很不错的。
小改一下。:)

private y(51) as long

sub gen()
dim n as long,s as long,a as long,b as long
for n=0 to 51
'y(51)=n+1 <==这里
y(n)=n+1
next

for n=1 to 80
a=int(rnd*51):b=int(rnd*51)
s=y(a):y(a)=y(b):y(b)=s
next

end sub
nik_Amis 2003-03-21
  • 打赏
  • 举报
回复
private y(51) as long

sub gen()
dim n as long,s as long,a as long,b as long
for n=0 to 51
y(51)=n+1
next

for n=1 to 80
a=int(rnd*51):b=int(rnd*51)
s=y(a):y(a)=y(b):y(b)=s
next

end sub
这个算法速度快得多

AechoJohn 2003-03-21
  • 打赏
  • 举报
回复
楼上的
Cooly 2003-03-21
  • 打赏
  • 举报
回复
'生成52个1到100的不重复随机数
Dim i, j As Long
Dim ran(1 To 52) As Long
Dim tNum As Long
Dim isExist As Boolean
Randomize
For i = 1 To 52
isExist = False

tNum = Int(100 * Rnd + 1)
Debug.Print tNum
For j = 1 To i
If ran(j) = tNum Then
isExist = True
i = i - 1
Exit For
End If
Next

If isExist = False Then
ran(i) = tNum
End If

Next

For i = 1 To 52
msg = msg & ran(i) & Space(1)
Next
MsgBox msg
northwolves 2003-03-21
  • 打赏
  • 举报
回复
Private Sub Command1_Click()
Dim x As New Collection, a(0 To 51) As String
For i = 0 To 99
x.Add i
Next
For i = 0 To 51
Randomize
num = Int(Rnd * x.Count + 1)
a(i) = Str(x.Item(num))
x.Remove num
Next
MsgBox Join(a(), ","), 64, "52 random number between 0 and 99"
End Sub
Cooly 2003-03-21
  • 打赏
  • 举报
回复
不错不错。 :)
向Nik学习,精益求精。

pcwak 2003-03-21
  • 打赏
  • 举报
回复
UP
litsnake1 2003-03-21
  • 打赏
  • 举报
回复
相当不错,这个其实蛮巧妙

7,762

社区成员

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

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