# Re: 简单问题: 如果有20个数字,1 To 20,如何可以Random地出现不同次序?

Posted by knight on November 16, 1998 at 09:30:35:

In Reply to: Re: 简单问题: 如果有20个数字,1 To 20,如何可以Random地出现不同次序? posted by 匡 on November 12, 1998 at 17:07:31:

: : 既然你说是简单问题，就再想一想吧! 把你的方法 post 出来让大家参考。
: : 我是有一个办法，不过下周再告诉你。

: 如困是7个数字,最简单的方法....,但要排列20个数字就麻烦了,怎么办?

: Do
: Randomize
: am% = Int(7 * Rnd)

: bm% = Int(7 * Rnd)

: cm% = Int(7 * Rnd)

: dm% = Int(7 * Rnd)

: em% = Int(7 * Rnd)

: Loop Until am% <> bm% And am% <> cm% And am% <> dm% And am% <> em% And bm% <> cm% And bm% <> dm% And bm% <> em% And cm% <> dm% And cm% <> em% And dm% <> em%
以下是以交换法达成不重覆乱数
Option Explicit
Const Random_Num = 20
Dim NUM(Random_Num) As Long
End Sub
Private Sub Form_Load()
Dim Change_Num1&, Change_Num2&, Change_Buf&, Count_Num&
Randomize
For Count_Num = 0 To Random_Num
NUM(Count_Num) = Count_Num
Next
For Count_Num = 0 To Random_Num
Change_Num1 = Int(Rnd * Random_Num) + 1
Change_Num2 = Int(Rnd * Random_Num) + 1
Change_Buf = NUM(Change_Num1)
NUM(Change_Num1) = NUM(Change_Num2)
NUM(Change_Num2) = Change_Buf
Next
End Sub
先在阵列中放入0-20
再以乱数交换任意两数

Follow Ups: