求助控制台生成1-20随机数,生成10个,不能重复
var num = Enumerable.Range(1, 20)
.Select(x => new { v = x, k = Guid.NewGuid().ToString() }).ToList()
.OrderBy(x => x.k)
.Select(x => x.v)
.Take(10).ToList();
foreach (var i in num)
{
Console.WriteLine(i);
}
for(int i=0;i<=10;i++)
{
Random Rdm = new Random();
int iRdm = Rdm.Next(1, 20);
if(iRdm==i)
{
continue;
}
}
int[] Source = new int[20];
for (int i = 1; i <= 20; i++)
{
Source[i - 1] = i;
}
int[] Result = new int[10];
for (int i = 1; i <= 10; i++)
{
Result[i - 1] = new Random().Next(Guid.NewGuid().GetHashCode());
}
C# code:
private static int[] GetRandom(int minValue, int maxValue, int count)
{
Random rnd = new Random();
int length = maxValue - minValue + 1;
byte[] keys = new byte[length];
rnd.NextBytes(keys);
int[] items = new int[length];
for (int i = 0; i < length; i++)
{
items[i] = i + minValue;
}
Array.Sort(keys, items);
int[] result = new int[count];
Array.Copy(items, result, count);
return result;
}
public static void Main()
{
// Example Array
int[] arr1 = GetRandom(1, 800, 50);
}