C# n从源数组中提取随机值: string[] addetailsID = new string[20]; // this is the array I want to index into // generate the 4 unique indices into addetailsID Random random = new Random(); List<int> indices = new List<int>(); while (indices.Count < 4) { int index = random.Next(0, addetailsID.Length); if (indices.Count == 0 || !indices.Contains(index)) { indices.Add(index); } } // now get the 4 random values of interest string[] strAdDetailsID = new string[4]; for (int i = 0; i < indices.Count; i++) { int randomIndex = indices[i]; strAdDetailsID[i] = addetailsID[randomIndex]; } string[]addetailsID=新字符串[20];//这是我要索引到的数组 //将4个唯一索引生成addetailsID 随机=新随机(); 列表索引=新列表(); 而(指数计数

C# n从源数组中提取随机值: string[] addetailsID = new string[20]; // this is the array I want to index into // generate the 4 unique indices into addetailsID Random random = new Random(); List<int> indices = new List<int>(); while (indices.Count < 4) { int index = random.Next(0, addetailsID.Length); if (indices.Count == 0 || !indices.Contains(index)) { indices.Add(index); } } // now get the 4 random values of interest string[] strAdDetailsID = new string[4]; for (int i = 0; i < indices.Count; i++) { int randomIndex = indices[i]; strAdDetailsID[i] = addetailsID[randomIndex]; } string[]addetailsID=新字符串[20];//这是我要索引到的数组 //将4个唯一索引生成addetailsID 随机=新随机(); 列表索引=新列表(); 而(指数计数,c#,c#-2.0,C#,C# 2.0,首先需要生成4个唯一索引,然后从源数组中提取随机值: string[] addetailsID = new string[20]; // this is the array I want to index into // generate the 4 unique indices into addetailsID Random random = new Random(); List<int> indices = new

首先需要生成4个唯一索引,然后从源数组中提取随机值:

        string[] addetailsID = new string[20];  // this is the array I want to index into
        // generate the 4 unique indices into addetailsID
        Random random = new Random();
        List<int> indices = new List<int>();
        while (indices.Count < 4)
        {
            int index = random.Next(0, addetailsID.Length);
            if (indices.Count == 0 || !indices.Contains(index))
            {
                indices.Add(index);
            }
        }
        // now get the 4 random values of interest
        string[] strAdDetailsID = new string[4];
        for (int i = 0; i < indices.Count; i++)
        {
            int randomIndex = indices[i];
            strAdDetailsID[i] = addetailsID[randomIndex];
        }
string[]addetailsID=新字符串[20];//这是我要索引到的数组
//将4个唯一索引生成addetailsID
随机=新随机();
列表索引=新列表();
而(指数计数<4)
{
int index=random.Next(0,addtailsid.Length);
如果(index.Count==0 | |!index.Contains(index))
{
索引。添加(索引);
}
}
//现在获取感兴趣的4个随机值
string[]strAdDetailsID=新字符串[4];
对于(int i=0;i
查找洗牌技术(搜索Knuth、Fisher-Yates)。洗牌整个数组,然后按顺序选择四个。要获得唯一值,我认为您需要存储到目前为止获得的
索引的列表,并使用它检查以前是否使用过新索引查找洗牌技术(搜索Knuth,Fisher-Yates)。洗牌整个数组,然后按顺序选择四个。要获得唯一值,我认为您需要存储到目前为止获得的
索引列表,并使用它检查以前是否使用过新索引。。。错过了C#2.0的限制。这里的代码不起作用,但概念会起作用。哦。。。错过了C#2.0的限制。这里的代码不起作用,但概念会起作用。
result = []
For i = 0 to numItemsRequired:
    randomIndex = random number between i and source.length - 1
    result.add(source[randomIndex])
    swap(source[randomIndex], source[i])
string[] strAdDetailsID = new string[4];
Random rand = new Random();

for (int i = 0; i < 4; i++)
{
    int randIndex = rand.Next(i, adDetailsID.Length);
    strAddDetails[i] = adDetailsID[randIndex];

    string temp = adDetailsID[randIndex];
    adDetailsID[randIndex] = adDetailsID[i];
    adDetails[i] = temp;
}
 List<string> addetailsID = new List<string>{"1","2","3","4","5","6"};

 string[] strAdDetailsID = new string[4];
 Random random = new Random();
 for (int i = 0; i < 4; i++)
 {

     int index = random.Next(0, addetailsID.Count);
     string value = addetailsID[index].ToString();
     addetailsID.RemoveAt(index);
     strAdDetailsID[i] = value;
 }

  strAdDetailsID.Dump();
List<string> list = new List<string>() { "There", "Are", "Many", "Elements", "To", "Arrays" };

foreach (var item in list.OrderBy(f => Guid.NewGuid()).Distinct().Take(4))
{
    Console.WriteLine(item);
}
        string[] addetailsID = new string[20];  // this is the array I want to index into
        // generate the 4 unique indices into addetailsID
        Random random = new Random();
        List<int> indices = new List<int>();
        while (indices.Count < 4)
        {
            int index = random.Next(0, addetailsID.Length);
            if (indices.Count == 0 || !indices.Contains(index))
            {
                indices.Add(index);
            }
        }
        // now get the 4 random values of interest
        string[] strAdDetailsID = new string[4];
        for (int i = 0; i < indices.Count; i++)
        {
            int randomIndex = indices[i];
            strAdDetailsID[i] = addetailsID[randomIndex];
        }