首页 > 代码库 > Random 生成不重复的随机数

Random 生成不重复的随机数


Hashtable ha = new Hashtable();
Random rm = new Random();

for (int i = 0; ha.Count < 4; i++)
{
  int number = rm.Next(1,10);
if (!ha.ContainsValue(number))
{
  ha.Add(number, number);

  Console.WriteLine(number.ToString());
}
}

Random 生成不重复的随机数