Random ra = new Random(DateTime.Now.Millisecond);生成随机数 Random.Next (Int32, Int32)
int[] arr=new int[10]; List<int> lst = new List<int>(); while (true) { if (lst.Count == 10) { break; } Random R1 = new Random(unchecked((int)System.DateTime.Now.Ticks)); int i = R1.Next(1, 100); if (lst.Contains(i)) { continue; } lst.Add(i); } lst.CopyTo(arr, 0);
static void Main(string[] args) { ArrayList AList = new ArrayList();//定义可变长数组 Random rd = new Random(); int num;//将每次产生的随机数赋给此变量 bool _bool = true;//判断是否有重复,初始时,数组中没有元素,不需要判断,固赋初值为True //产生8个不同的随机数 while (AList.Count < 8) { num = rd.Next(0, 9); for (int i = 0; i < AList.Count; i++) { //如果重复,跳出此循环,重新产生随机数 if (num == Convert.ToInt32(AList[i])) { _bool = false; break; } //如果与数组中该项不同,则为True else { _bool = true; } } //上面循环后,如果_bool仍为True,则说明此次产生的随机数在数组中无重复,则将此随机数赋给数组 if (_bool == true) { AList.Add(num.ToString()); } } //打印输出 for (int i = 0; i < AList.Count; i++) { Console.WriteLine(AList[i]); } Console.ReadLine(); }
int Count = 0; List<int> r = new List<int>(); List<int> Result = new List<int>(); int MaxCount = 30; while (true) { int i = new Random().Next(1, 100); if (!r.Contains(i)) { r.Add(i); Count++; } if (Count >= MaxCount) break; } Result.AddRange(r.OrderBy(i => i)); Console.ReadKey(); }
System.Random R = new Random(unchecked((int)System.DateTime.Now.Ticks)); List<int> L = new List<int>(); int Count =0; int Temp = 0; for (int i = 0; i < 10; i++) { Again: Temp = R.Next(); if (L.Contains(Temp)) { goto Again; } else { L.Add(Temp); Count += 1; } } int[] Ro = L.ToArray(); foreach (int item in Ro) { Console.Write("{0} ", item); }
Random.Next (Int32, Int32)
int[] arr=new int[10];
List<int> lst = new List<int>();
while (true)
{
if (lst.Count == 10)
{
break;
}
Random R1 = new Random(unchecked((int)System.DateTime.Now.Ticks));
int i = R1.Next(1, 100);
if (lst.Contains(i))
{
continue;
}
lst.Add(i);
}
lst.CopyTo(arr, 0);
{
ArrayList AList = new ArrayList();//定义可变长数组
Random rd = new Random();
int num;//将每次产生的随机数赋给此变量
bool _bool = true;//判断是否有重复,初始时,数组中没有元素,不需要判断,固赋初值为True
//产生8个不同的随机数
while (AList.Count < 8)
{
num = rd.Next(0, 9);
for (int i = 0; i < AList.Count; i++)
{
//如果重复,跳出此循环,重新产生随机数
if (num == Convert.ToInt32(AList[i]))
{
_bool = false;
break;
} //如果与数组中该项不同,则为True
else
{
_bool = true;
}
}
//上面循环后,如果_bool仍为True,则说明此次产生的随机数在数组中无重复,则将此随机数赋给数组
if (_bool == true)
{
AList.Add(num.ToString());
}
} //打印输出
for (int i = 0; i < AList.Count; i++)
{
Console.WriteLine(AList[i]);
}
Console.ReadLine();
}
C#生成指定数目的互不相同的随机数http://hi.baidu.com/feifeiyaqi3/blog/item/6e1d86170b72744321a4e926.html
List<int> r = new List<int>();
List<int> Result = new List<int>();
int MaxCount = 30;
while (true)
{
int i = new Random().Next(1, 100);
if (!r.Contains(i))
{
r.Add(i);
Count++;
}
if (Count >= MaxCount)
break;
}
Result.AddRange(r.OrderBy(i => i)); Console.ReadKey();
}
System.Random R = new Random(unchecked((int)System.DateTime.Now.Ticks));
List<int> L = new List<int>();
int Count =0;
int Temp = 0; for (int i = 0; i < 10; i++)
{
Again:
Temp = R.Next();
if (L.Contains(Temp))
{
goto Again;
}
else
{
L.Add(Temp);
Count += 1;
}
} int[] Ro = L.ToArray();
foreach (int item in Ro)
{
Console.Write("{0} ", item);
}
应注释掉: //int Count =0;
//Count += 1;