-2

I have an array with 100 elements, of which I randomly pick 4 (the same element can't be picked twice). I repeat this process many times, to try and get every possible combination. There must be a more efficiënt way though.

How would I go about creating a loop that just creates every possible combination?

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++;
}

Edit: the question that was linked to as duplicate is different, as it allows for duplicate elements to be chosen.

Found my anwser here: https://stackoverflow.com/a/17871949/1880554

  • There is an existing question on Algo about all permutation/subset of size N. 1 virtual token to the one that find it – xdtTransform Apr 19 '19 at 08:57
  • 1
    Every possible combination of what? Those randomly picked 4 elements? Those 4 elements against 100 other elements? – Adrian Apr 19 '19 at 09:00
  • 1
    Could you show us what you have tried yourself? This kind of feels like we're doing your homework. – Jordec Apr 19 '19 at 09:01
  • @xdtTransform, here you are https://stackoverflow.com/questions/127704/algorithm-to-return-all-combinations-of-k-elements-from-n – Drag and Drop Apr 19 '19 at 09:11
  • @Jordec Added code, my "homework" is done, the code works, I'm just trying to make it more efficiënt. – user1880554 Apr 19 '19 at 09:21
  • If by "duplicate element" you mean "different elements that happen to have the same value" (e.g. your input is ABCDA, with the first and last element having the same value) then just remove the duplicates from the input before you generate the combinations. – Matthew Watson Apr 19 '19 at 09:54
  • By the way, the answer marked as a duplicate does NOT allow duplicate elements to be chosen, since it is producing combinations. If the elements of the input collection have duplicate values, then the output may contain duplicate values but not duplicate elements. As I say above, you can remove duplicate values from the input if they exist. – Matthew Watson Apr 19 '19 at 10:01
  • @Matthew Watson, TT FF UU is not allowed as explained in my original post, because it would mean picking the same element twice. Also picking TF would exclude FT from being picked later as they are the same thing. I've never talked about values, that's why I specified it being about elements from an array. So if the array had a length of 4 instead of 100 there would only be a single result (0, 1, 2, 3) in any order. – user1880554 Apr 19 '19 at 11:16

1 Answers1

0

When you say that the same element can't be picked twice, you mean that you can't have , for example, (1 , 2, 3, 2) since "2" already exists? If the order matters of your generated elements, and there CAN"T be repititions, it is called Permutation without repitition. Here is a reference to the theory : https://www.mathsisfun.com/combinatorics/combinations-permutations.html About your case with the looping, this might help: Permutation without repetition C#

Edwardcho Vaklinov
  • 118
  • 1
  • 1
  • 11
  • Yes this is what I meant – user1880554 Apr 19 '19 at 09:12
  • The order doesn't matter by the way, so selecting elements (0, 1, 2, 3) is the same as (3, 2, 1, 0) or (1, 2, 0, 3), as it's still the same elements that will get compared. Only things like (1, 2, 3, 2) are disallowed, as it's comparing one of the elements with itself. – user1880554 Apr 19 '19 at 09:40