VB.net 如何产生不重复的随机整数

2025-01-18 19:03:19
推荐回答(4个)
回答1:

这个问题我刚解决了,我用了一种最笨的办法,就是把数的范围存入一个list中,在窗体上建了两个list,几个label,还有一个timer,两个Command
Private Sub Command1_Click()
Timer1.Enabled = True
End Sub

Private Sub Command2_Click()
Timer1.Enabled = False
'List1.RemoveItem Label1.Caption
'List2.AddItem Label1.Caption
For j = 0 To List1.ListCount - 1
If List1.List(j) = Label1.Caption Then

List2.AddItem Label1.Caption, 0
List1.RemoveItem j
Exit Sub
End If

Next j
End Sub

Private Sub Form_Load()

For i = 0 To 38
List1.List(i) = Format(i + 1, "00")
Next i
End Sub

Private Sub Timer1_Timer()
Label1.Caption = List1.List(Rnd * (List1.ListCount - 1))
End Sub

回答2:

'我在VB6.0中一般是这样处理的
St!=Timer
for i=0 to St!
Rnd
next i

回答3:

Randomize()
For i = 1 To n
ret=0
numDl(i) = Int(48 * Rnd() + 1)
for a=1 to i
if numDl(i)=numDl(a) then
ret=1
exit for
else
ret=0
end if
next
loop while ret=1
Console.Write("{0} ", numDl(i))
Next i

回答4:

Randomize(Timer)