# C#随机取数怎样消除重复数字（求代码）？

0

C#随机取数消除重复数字的方法（求代码）。

0

class UniqueRandom : Random
{
Dictionary<int, int> _tbl = new Dictionary<int, int>();

new public int Next()
{
int ret = base.Next();

while (_tbl.ContainsKey(ret))
{
ret = base.Next();
}

return ret;
}

new public int Next(int maxValue)
{
if (_tbl.Count >= maxValue)
{
throw new Exception("Overflow!");
}

int ret = base.Next(maxValue);

while (_tbl.ContainsKey(ret))
{
ret = base.Next(maxValue);
}

return ret;
}

new public int Next(int minValue, int maxValue)
{
if (_tbl.Count >= maxValue - minValue)
{
throw new Exception("Overflow!");
}

int ret = base.Next(minValue, maxValue);

while (_tbl.ContainsKey(ret))
{
ret = base.Next(minValue, maxValue);
}

return ret;
}
}

static void Main(string[] args)
{
UniqueRandom rand = new UniqueRandom();

for (int i = 0; i < 100; i++)
{
Console.WriteLine(rand.Next(100));
}

}

using System;
using System.Collections.Generic;
using System.Text;

namespace TestConsole
{
class UniqueRandom : Random
{
System.Collections.Generic.HashSet<int> _tbl = new HashSet<int>();

new public int Next()
{
int ret = base.Next();

while (_tbl.Contains(ret))
{
ret = base.Next();
}

return ret;
}

new public int Next(int maxValue)
{
if (_tbl.Count >= maxValue)
{
throw new Exception("Overflow!");
}

int ret = base.Next(maxValue);

while (_tbl.Contains(ret))
{
ret = base.Next(maxValue);
}

return ret;
}

new public int Next(int minValue, int maxValue)
{
if (_tbl.Count >= maxValue - minValue)
{
throw new Exception("Overflow!");
}

int ret = base.Next(minValue, maxValue);

while (_tbl.Contains(ret))
{
ret = base.Next(minValue, maxValue);
}

return ret;
}
}
}

eaglet | 专家六级 |园豆：17139 | 2009-04-09 20:03

0
`Code            IList<int> list = new List<int>();            Random rd = new Random();            for (int i = 0; i < 100; i++)            {                int num = rd.Next(0, 100);                if (!list.Contains(num))                {                    list.Add(num);                }            }            foreach (int i in list)            {                Console.WriteLine(i);            }`

0

Gray Zhang | 园豆：17610 (专家六级) | 2009-04-09 22:27
0

/// <summary>
/// 生成随机的字母
/// </summary>
private string RndNum(int VcodeNum)
{
string Vchar = "0,1,2,3,4,5,6,7,8,9";
string[] VcArray = Vchar.Split(',');
string VNum = "";
int temp = -1;
Random rand = new Random();
for (int i = 1; i < VcodeNum + 1; i++)
{
if (temp != -1)
{
rand = new Random(i * temp * unchecked((int)DateTime.Now.Ticks));
}
int t = rand.Next(VcArray.Length);
if (temp != -1 && temp == t)
{
return RndNum(VcodeNum);
}
temp = t;
VNum += VcArray[t];
}
return VNum;
}

0

public static Array NoRepeatInsert()
{
System.Collections.ArrayList arrlist = new System.Collections.ArrayList();
int[] arr = new int[100];
Random rdm = new Random();
for (int i = 0; i < 100; i++)
{
//arrlist[i] = i + 1;
}
for (int j = 0; j <100; j++)
{
int index = rdm.Next(arrlist.Count);//0~arrlist.Count-1
arr[j] =Convert.ToInt32(arrlist[index]);
arrlist.RemoveAt(index);//At

}
return arr;
}

vasty | 园豆：75 (初学一级) | 2009-04-10 16:38

您需要登录以后才能回答，未注册用户请先注册