-2

I'm trying to select certain amount of files randomly from a folder. For example if a folder has 100 files, I want to select 40 files randomly rather than the first 40.

            string sourceFolder = //unc path;
            var dir = new DirectoryInfo(sourceFolder );
            var allFiles = dir.GetFiles("*.pdf");
            int fileCount = allFiles.Length; // 100 files
            int folderOne = 60;
            int folderTwo = 40;

            if (fileCount > 0)
            {
                // select 60 files randomly and move them to folderOne
            }

I tried using Random function in C# but I cannot get my head around it.

             var random = new Random();
             int index = random.Next(0, fileCount - 1);
             var file = allFiles[index].FullName;

Any help would be greatly appreciated. Thanks

kas_miyulu
  • 335
  • 1
  • 5
  • 27
  • HI KMR. Are you asking how to generate a random number up to a certain size? Or are you asking how to randomly select some files? – Casey Crookston Sep 30 '20 at 20:03
  • @CaseyCrookston I'm after how to randomly select certain amount of files. For example not 1 file from a folder but select 40 files randomly out of 100 files. – kas_miyulu Sep 30 '20 at 20:05
  • This may answer your question: https://stackoverflow.com/questions/742685/select-random-file-from-directory/742690 – Casey Crookston Sep 30 '20 at 20:08
  • @CaseyCrookston I saw this question. but my problem is bit different. On the question in your link, he is selecting 1 file from a folder. Is there any way that I can select 40 random files at once? – kas_miyulu Sep 30 '20 at 20:10
  • `I tried using Random function in C#` you left that part out of the code in the question – Ňɏssa Pøngjǣrdenlarp Sep 30 '20 at 20:11
  • Take the code in that other question and wrap it in a Do While loop. Just repeat the same process 40 times. – Casey Crookston Sep 30 '20 at 20:13
  • @ŇɏssaPøngjǣrdenlarp I have edited the question and added the Random function. The reason I didn't add it because i thought it was incorrect – kas_miyulu Sep 30 '20 at 20:31
  • 1
    @CaseyCrookston I used David's linq query to make the list random. I've tested my code and it works. Unfortunately I can't post my answer as the question is closed – kas_miyulu Sep 30 '20 at 20:33
  • Glad you got it working. – Casey Crookston Sep 30 '20 at 20:35

1 Answers1

2

Randomize the order of the files, then you can simply copy the first 60 to one location, and the last forty somewhere else.

eg

var rnd = new Random();
var shuffled = allFiles.OrderBy(f => rnd.Next()).ToList();
David Browne - Microsoft
  • 80,331
  • 6
  • 39
  • 67
  • 2
    Not truly a random shuffle. But it might work for OP's purposes. – Casey Crookston Sep 30 '20 at 20:20
  • Brill! this is exactly what I was after. It worked – kas_miyulu Sep 30 '20 at 20:30
  • 1
    @CaseyCrookston why not truly random? Because of the pseudo-random generator used? – Theodor Zoulias Oct 01 '20 at 02:49
  • @TheodorZoulias, yes that too. I just meant that this will indeed shuffle the list, but not in a truly random way. "Random" implies that all unique combinations will occur with an even distribution. If you were to run this 1,000 times, I suspect you would see the same results repeat. But again, it's a non-issue, as it gives the OP what he needs. – Casey Crookston Oct 01 '20 at 17:08
  • @CaseyCrookston yeap, the `Random` class can be initialized with "only" 2^32 different states (its seed is `Int32`). So if my math are correct, you should expect to get a duplicate after 2^16 (65,536) shufflings on average. – Theodor Zoulias Oct 01 '20 at 18:45
  • It's not obvious that this algorithm is actually a random shuffle. I think it is, ie each position is equally likely to end up in any position. But it is entirely reasonable to require a proof, or substituting a well-known and faster algorithm. EG see: https://stackoverflow.com/questions/1287567/is-using-random-and-orderby-a-good-shuffle-algorithm – David Browne - Microsoft Oct 01 '20 at 19:10
  • @CaseyCrookston ...or you can initialize a `Random` instance only once, and [save its state](https://stackoverflow.com/questions/19512210/how-to-save-the-state-of-a-random-generator-in-c) after each shuffling so that can you continue using the same instance across multiple executions of the application. In this case you'll probably exhaust the [periodicity](https://stackoverflow.com/questions/42739296/random-next-how-many-iterations-before-a-wrap-around-occurs) of the `Random` before getting the first duplicate shuffling. – Theodor Zoulias Oct 01 '20 at 19:14