Re: [問題] 如何設定亂數不出現指定數字已回收
※ 引述《kofpopo (深遂心)》之銘言:
: 現在我利用 rand 取ㄧ亂數範圍為 0-50
: 但亂數不出現 0、4、15等數字
: 請教各位先進如何撰寫
你先建一個矩陣
A = [ 1 2 3 5 6 7 8 9 10 11 12 13 14 16 17 18 ... 50 ]
這樣只會有48個元素
然後隨機取1~48範圍之間的數字
接著把取出來的數字當作A矩陣的index來用
譬如隨機跑出15
那麼從A矩陣裡拿出來用的數字就是17
--
Deserves death! I daresay he does. Many that live deserve death. And some die
that deserve life. Can you give that to them? Then be not too eager to deal out
death in the name of justice, fearing for your own safty. Even the wise cannot
see all ends.
Gandalf to Frodo
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 220.131.229.16
推
06/19 21:06, , 1F
06/19 21:06, 1F
推
06/20 20:23, , 2F
06/20 20:23, 2F
→
06/21 10:44, , 3F
06/21 10:44, 3F
討論串 (同標題文章)
本文引述了以下文章的的內容:
完整討論串 (本文為第 2 之 3 篇):