方法名:GetArray(int[] arr,int count)
作用:在给与这定的arr数组中随机选取count数量的整数,返回其数组
问题:如何写这个方法
例:arr[0]=1
arr[1]=3
arr[2]=5
....
....
arr[6]=13
count=2在arr中随机选择两个数并返回一个数组
作用:在给与这定的arr数组中随机选取count数量的整数,返回其数组
问题:如何写这个方法
例:arr[0]=1
arr[1]=3
arr[2]=5
....
....
arr[6]=13
count=2在arr中随机选择两个数并返回一个数组
public static ListDTO[] Shuffle(ListDTO[] listDTO)
{
int newIndex;
ListDTO tempListDTO; try
{
for (int i = 0; i < listDTO.Length; i++)
{
newIndex = new Random().Next(listDTO.Length);
tempListDTO = listDTO[i];
listDTO[i] = listDTO[newIndex];
listDTO[newIndex] = tempListDTO;
}
}
catch (Exception ex)
{
LoggerUtility.GetInstance().ApplicationLogger.Error("Error while shuffling ListDTO objects", ex);
} return listDTO;
}
可以先对数组做任意的排序,然後再取出你所要的数目即可
需要添加什么引用??
int [] GetArray(int[] arr,int count)
{
int[] arr0 = new int[count];
Random rd = new Random();
int k=0;
while (k < count)
{
int i = rd.Next(0, arr[arr.Count()-1]);
if (!arr0.ToList().Contains(i))
{
arr0[k] = i;
k++;
}
}
return arr0;
}
{
Func<int,int[],bool > IsDump=(p,t)=>
{
return t.Contains (p )?true:false;
}; int[] target = new int[count];
Random random = new Random();
for (int i = 0; i < count; i++)
{
int tmp=random.Next (0,arr.Length -1);
while (IsDump (arr[tmp] ,target ))
{
tmp=random.Next (0,arr.Length -1);
}
target[i] = arr[tmp];
}
return target;
} static void Main(string[] args)
{
int[] target = GetArray(new int[] { 1, 2, 3, 4, 23, 2, 2, 31, 1, 3, 2, 1, 1, 1, 2, 5, 3, 6 }, 3);
Console.ReadKey();
}