Random File Selection from Directory

  • Thread starter Thread starter Baz
  • Start date Start date
B

Baz

I want to randomly select some files from a source directory & copy them to
a target folder for testing. How can I select files from the source
directory randomly?

I have thought about getting a count of the total files in the folder & then
using a random number generator to select files based on count. Is there
another more efficient way of doing this, something that doesn't involve
generating a random number?

thanks!
 
Baz said:
I want to randomly select some files from a source directory & copy them to
a target folder for testing. How can I select files from the source
directory randomly?

I have thought about getting a count of the total files in the folder & then
using a random number generator to select files based on count. Is there
another more efficient way of doing this, something that doesn't involve
generating a random number?

How do you expect to get a random result without generating a random
number? Why do you think that generating a random number will be
inefficient?
 
Hi,

Honestly I don;t understand the logic behind this test, but what you can do
use Directory.GetFiles( ) this return a string[]
then you can generate numbers between 0 and the array length using
Random.Next( array.Length )

Note that nothing prevent that you get the same element twice!

Cheers,
 
How do you expect to get a random result without generating a random
number? Why do you think that generating a random number will be
inefficient?

Hmm, I think it had something to do with the presence of "using
System.Threading" in the sample code that MSDN provides for the Random
number class...
 
Baz said:
Hmm, I think it had something to do with the presence of "using
System.Threading" in the sample code that MSDN provides for the Random
number class...

That's just to allow the current thread to sleep so that the seed for
the next random number generator created will be different.
 
Back
Top