-3

I made an array containing 24 elements. I want to iterate a loop 24 times, each time taking all the elements from the array and making a unique sequence.

For example:

for(int i=0; i<4 ; i++) 
 array = [a,b,c,d]

The output I want is:

iteration1: abcd
iteration2: acdb
iteration3: bacd
iteration4: dcab

The specific order is not important. What's important is the sequence for each iteration must be unique.

Can somebody recommend a way to this? Perhaps there is some function for randomisation in C#?

Joel Coehoorn
  • 399,467
  • 113
  • 570
  • 794
  • 2
    What have you tried? What didn't work? – Daxtron2 Apr 12 '18 at 18:33
  • 2
    Please show us what you have tried so far. – JuanR Apr 12 '18 at 18:34
  • 2
    Code! Feed us code! – Callat Apr 12 '18 at 18:35
  • Basically you want random permutations. Eric Lippert wrote a blog about how to do produce the permutation, then you can just add the randomness yourself https://ericlippert.com/2013/04/15/producing-permutations-part-one/ – juharr Apr 12 '18 at 18:35
  • 2
    This was not a duplicate of the question is was closed for. It **seems** to be about just randomizing a sequence, but when you look closer it's really about permutations, which is a different question. There's probably already a good dupe out there, but we need to find that one to use if we're gonna close this one. – Joel Coehoorn Apr 12 '18 at 18:42
  • @JoelCoehoorn I don't see good duplicate about *random permutation* (all I find are generating sequential permutations)... Unfortunately it took me too long to click "edit" and add permutation duplicate in addition to shuffle... – Alexei Levenkov Apr 12 '18 at 18:47

1 Answers1

1

When you need individual sequences to be unique, it's no longer good enough for things to be truly random. True randomness would allow you to repeat sequences. What you need instead are called permutations.

I don't have time right now to going into all the details, but Eric Lippert (formerly on the C# language team at Microsoft) has a series of blog posts with some great information on doing permutations in C#:

https://ericlippert.com/2013/04/15/producing-permutations-part-one/

Read through all the parts. It's part three before he gets to any code, but you need parts one and two to understand what he's doing.

It's also worth noting here that the individual permutations are likely to come out somewhat ordered. To get what you really need, you'll likely need to generate all permutations, shuffle the set of permutation results, and then take the first 24 entries from the shuffled results.

That sounds... expensive. This is why it's important to read and understand what's happening in the earlier articles. If you can understand how it works, you may be able to randomize your output to some degree as you go, and thus greatly increase the efficiency.

That's also the formal computer-sciencey solution. Since the size of your problem set seems small, you may also be able to do a more practical solution. Clone the array 24 times (possibly to a 2D array) and then loop through the clones. For each loop iteration, you shuffle that clone, as well as check it against prior clones to make sure it's unique, repeating as necessary. For larger sets this will be slower; probably much slower. But at your size (24x24), this should be a win.

Joel Coehoorn
  • 399,467
  • 113
  • 570
  • 794
  • You don't really need to read :) https://stackoverflow.com/questions/11208446/generating-permutations-of-a-set-most-efficiently already provides the all code for permutations, but to get *random* permutation you'd need to either skip random number of them... Or also simply shuffle https://stackoverflow.com/questions/273313/randomize-a-listt list and checking if same combination already generated is fine for small number of iterations. – Alexei Levenkov Apr 12 '18 at 18:40