作者clifflu ( What for ?)
看板Visual_Basic
标题Re: [.NET] 欲使产生之乱数不重复的问题!
时间Tue Apr 11 08:13:54 2006
※ 引述《zero1590 (小白)》之铭言:
: 各位前辈好,我想产生4个乱数,
: 每个乱数不能一样,
: 我想了一个晚上,
: 做出来以下的程式...
: dim a,i,j as integer
: dim take() as integer
: Randomize()
: For a = 0 To 3
: take(a) = Int(Rnd() * 10) + 1
: Next
: For i = 3 To 1 Step -1
: For j = i - 1 To 0 Step -1
: Do While take(i) = take(j)
: take(j) = Int(Rnd() * 10) + 1
: Loop
: Next
: Next
: label1.text=take(0) & " " & take(1) & " " & take(2) & " " & take(3)
如果把流程改掉呢 ?
一次取得一个数,重取直到与前面都不相同 ?
dim a, j as integer
dim take() as integer
dim isok as boolean ' true if ok
randomize
take(a) = int(rnd() * 10) + 1
for a = 1 to 3 ' 确定写入 take(a)
do
isok = true
take(a) = int(rnd() * 10) + 1
for j = 0 to a - 1 ' 检查回圈
if take(a) = take(j) then
isok = false
end if
next j
loop while not isok
next a
--
鬼压床怎麽办
骑上去啊
--
※ 发信站: 批踢踢实业坊(ptt.cc)
◆ From: 140.112.217.134