C#中生成随机不重复数
答案:2 悬赏:0 手机版
解决时间 2021-04-03 12:30
- 提问者网友:记得曾经
- 2021-04-02 18:38
C#中生成随机不重复数
最佳答案
- 五星知识达人网友:轻熟杀无赦
- 2021-04-02 18:51
程序没有错,感觉可以更精简些。
Random rd = new Random();//随即初始化
int[] ar = new int[10];//10个整数的数组
int seti = 11;
for (int i = 0; i < seti - 1; )
{
int temp = rd.Next(seti);//生成随机数
if (ar.Where(e => e == temp).Count() <= 0) // 这个是lamda表达式,如果不会可以用for循环代替
{
ar[i] = temp;
++i;
}
}
for (int i = 0; i < seti - 1; i++)
{
Console.WriteLine((i + 1) + "是" + ar[i]);
}
Console.ReadKey();
Random rd = new Random();//随即初始化
int[] ar = new int[10];//10个整数的数组
int seti = 11;
for (int i = 0; i < seti - 1; )
{
int temp = rd.Next(seti);//生成随机数
if (ar.Where(e => e == temp).Count() <= 0) // 这个是lamda表达式,如果不会可以用for循环代替
{
ar[i] = temp;
++i;
}
}
for (int i = 0; i < seti - 1; i++)
{
Console.WriteLine((i + 1) + "是" + ar[i]);
}
Console.ReadKey();
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯