[英]Generate every combination of numbers
我有一個包含 100 個元素的數組,其中我隨機選擇了 4 個(同一元素不能被選擇兩次)。 我多次重復這個過程,以嘗試獲得所有可能的組合。 但必須有更有效的方法。
我將如何創建一個只創建所有可能組合的循環?
double[][] picked = new double[4][];
int[] chosen = new int[4];
Random rnd = new Random();
List<int> exclude = new List<int>();
int z = 0;
while (z < 4)
{
picked[z] = new double[rows];
int x = rnd.Next(0, rows);
if (exclude.Contains(x))
{
continue;
}
exclude.Add(x);
// do stuff with the chosen elements
z++;
}
編輯:鏈接到重復的問題是不同的,因為它允許選擇重復的元素。
在這里找到我的答案: https ://stackoverflow.com/a/17871949/1880554
當你說同一個元素不能被選擇兩次時,你的意思是你不能有,例如, (1 , 2, 3, 2) 因為“2”已經存在? 如果生成元素的順序很重要,並且不能重復,則稱為Permutation without repitition 。這是對理論的參考: https : //www.mathsisfun.com/combinatorics/combinations-permutations.html您的循環情況,這可能會有所幫助: Permutation without repeat C#
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.