Re: [問題] 如何設定亂數不出現指定數字已回收

看板MATLAB作者 (神無月 孝臣)時間15年前 (2010/06/19 20:45), 編輯推噓2(201)
留言3則, 3人參與, 最新討論串2/3 (看更多)
※ 引述《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
也可以用randperm
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
文章代碼(AID): #1C7BlQ0N (MATLAB)
文章代碼(AID): #1C7BlQ0N (MATLAB)