Re: [問題] 亂數產生的方式..

看板C_Sharp作者 (充滿自信與信任)時間14年前 (2010/03/19 15:30), 編輯推噓0(000)
留言0則, 0人參與, 最新討論串4/4 (看更多)
看到 danielguo 大的推文 才知道 Random.NextBytes() 這麼好用.. 就寫了一個來試試看.. 利用 Dictionary<Tkey, Tvalue> 檢查的方式就是 result.ContainsKey(key) 取值的方式就是 result[key] --------------------------------------------- Dictionary<string, byte[]> RandomBytes(int count, int ByteLength) { Dictionary<string, byte[]> result = new Dictionary<string, byte[]>(); Random random = new Random(); while (count > 0) { // 取亂數 byte[] t = new byte[ByteLength]; random.NextBytes(t); // 生成 key string key = ""; foreach (byte b in t) { key += b.ToString(); } if (!result.ContainsKey(key)) { result.Add(key, t); // 成功加入才會減 count--; } } return result; } ※ 引述《liaommx (Orz)》之銘言: : 最後我直接採用List的比對方法. : 先把產生的序列存入List, : 然後對List排序, (list.sort) : 然後跑 : for(i=0;i<List.count-1;i++) : { : if( List[m]==List[m++]) : { : writetofile(List[m]); : } : } : 這樣, : 比起I/O讀取快上很多, : 我想我的問題是在於,100萬筆資料,I/O要跑100萬*100萬 : 而這個方法只需要比對100萬次就好. -- ▇▃▂▁ ▁▂▃▇ ▃▆ ▆▃ ╦ ╗█▇▆▃ ▃▅▆▇╔╗ ◣∥◢║ ║ ╦ ╔ ╦╗ ╦ ╔ ╗ █◣▲ ▼▼▼▼ ║ ║ ║ ╠ ║ ║ ║ ◥█◤▲▲█◤ ║ ║ ╩ ╩ ╩ ╝╚ ╝ ψyzelly ╩ ╝▁▂▃▄▄ ▄ ▄▃▂▁╚╝ -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 60.251.142.155 ※ 編輯: MMAXo 來自: 60.251.142.155 (03/19 15:41)
文章代碼(AID): #1BeoWXSs (C_Sharp)
文章代碼(AID): #1BeoWXSs (C_Sharp)