如何从一个数组里 随机取6个数据出来

如题所述

不知道你要的是不是这个意思
private object[] GetRandomOfIndex(object[] p_DataList, int p_Count)
{
object[] _ReturnList = new object[p_Count];
System.Random _RandonIndex = new Random(System.DateTime.Now.Millisecond);
IList<int> _AddIndex = new List<int>();
while (true)
{
if (_AddIndex.Count == p_Count) return _ReturnList;
int _Index = _RandonIndex.Next(0, p_DataList.Length);
if (_AddIndex.IndexOf(_Index) == -1)
{
_ReturnList[_AddIndex.Count] = p_DataList[_Index];
_AddIndex.Add(_Index);
}
}
}

测试

string[] _Temp =new string[100];
for(int i=0;i!=_Temp.Length;i++)
{
_Temp[i]=i.ToString();
}

object[] _Value= GetRandomOfIndex(_Temp, 10);

string _MessageText = "";
for (int i = 0; i != _Value.Length; i++)
{
_MessageText += _Value[i].ToString()+",";
}
MessageBox.Show(_MessageText);
温馨提示:答案为网友推荐,仅供参考
第1个回答  2016-11-09
先随机出来六个不一样的索引,取这六个不一样的索引对应的数据就好了