Re: [.NET] 欲使產生之亂數不重複的問題!
※ 引述《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
討論串 (同標題文章)
本文引述了以下文章的的內容:
完整討論串 (本文為第 2 之 3 篇):
Visual_Basic 近期熱門文章
PTT數位生活區 即時熱門文章