作者MMAXo (充满自信与信任)
看板C_Sharp
标题Re: [问题] 乱数产生的方式..
时间Fri Mar 19 15:30:38 2010
看到 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)