C
C#2y ago
Meistro

❔ Random take a File from a Folder

Hello Dear CSharp Programmers I have a Question, lets say I have a folder with 1_000 Files. From this folder I would like to randomly pick 1_00. But every file should be taken only once. How would this method look like ? I tryed it by myself but it always take one file more then once. my Code:
static public int RandomNumber;
static public int randomIndex;
static Random random = new Random(DateTime.Now.Millisecond);;

public string GetRadomFileFromGivenFolderPath()
{
if (FolderPath_RandomFiles.Text != " ")
{
// Get an array of all file paths in the specified folder
string[] files = Directory.GetFiles(FolderPath_RandomFiles.Text);

// If no files are found, return null
if (files.Length == 0)
{
UpdateRichTextBox("No Files found!");
return "null";
}

// Generate a random number between 0 and the number of files minus 1
do
{
// Generate a random number between 0 and the number of files minus 1
randomIndex = random.Next(0, files.Length - 1);

} while (RandomNumber == randomIndex);

RandomNumber = randomIndex;
// Return the file path at the random index
return RandomFilePath = files[randomIndex];
}
else
{
UpdateRichTextBox("Folder path for \"random Files Path\" not found!");
return "null";
}
}
static public int RandomNumber;
static public int randomIndex;
static Random random = new Random(DateTime.Now.Millisecond);;

public string GetRadomFileFromGivenFolderPath()
{
if (FolderPath_RandomFiles.Text != " ")
{
// Get an array of all file paths in the specified folder
string[] files = Directory.GetFiles(FolderPath_RandomFiles.Text);

// If no files are found, return null
if (files.Length == 0)
{
UpdateRichTextBox("No Files found!");
return "null";
}

// Generate a random number between 0 and the number of files minus 1
do
{
// Generate a random number between 0 and the number of files minus 1
randomIndex = random.Next(0, files.Length - 1);

} while (RandomNumber == randomIndex);

RandomNumber = randomIndex;
// Return the file path at the random index
return RandomFilePath = files[randomIndex];
}
else
{
UpdateRichTextBox("Folder path for \"random Files Path\" not found!");
return "null";
}
}
I wanted to also store all ever picked Files and always check if the newly picked random file has already used once or not but I couldnt manage to code it. thank you for every help
14 Replies
Angius
Angius2y ago
Get all the files, shuffle them with Fischer-Yates algorithm, pick the first 100
Temptica
Temptica2y ago
that, or you simply save the numbers you already had before and check if it has been used before, if so, take a new number. sure, near the end it could happen that it says the same number multiple times but unless you need a very high efficient solution, this should do. can be done with only a few lines of extra code
Angius
Angius2y ago
Or use a list, and .RemoveAt() to remove the item, then get a new random index
Temptica
Temptica2y ago
even better solution tbh, if you don't mind them disappearing from your list ofcrs
Angius
Angius2y ago
Could always later leftovers.Concat(selected) to get the full list back
Temptica
Temptica2y ago
well, then you almost end up to my solution as you now have a list of files to save, instead of a list of numbers 😄 but could be slightly better still
Angius
Angius2y ago
True lol
Meistro
MeistroOP2y ago
ok thank you guys. So my code is completly wrong or is anything I could add to it so it those what you said ? xD
Angius
Angius2y ago
Well, your code doesn't guarantee picking all unique files in the first place It seems to guarantee that two of the same file won't be selected back to back, but nothing more than that
Temptica
Temptica2y ago
wouldn't call it wrong, just incomplete
Meistro
MeistroOP2y ago
might this one be correct ?
static public List<int> usedRandomNumbers = new List<int>();
static Random random = new Random(DateTime.Now.Millisecond);

public string GetRandomFileFromGivenFolderPath()
{
if (FolderPath_RandomFiles.Text != " ")
{
// Get an array of all file paths in the specified folder
string[] files = Directory.GetFiles(FolderPath_RandomFiles.Text);

// If no files are found, return null
if (files.Length == 0)
{
UpdateRichTextBox("No Files found!");
return "null";
}

// Generate a random number between 0 and the number of files minus 1
int randomIndex = -1;
do
{
randomIndex = random.Next(0, files.Length);
} while (usedRandomNumbers.Contains(randomIndex));

usedRandomNumbers.Add(randomIndex);

// Return the file path at the random index
return files[randomIndex];
}
else
{
UpdateRichTextBox("Folder path for \"random Files Path\" not found!");
return "null";
}
}
static public List<int> usedRandomNumbers = new List<int>();
static Random random = new Random(DateTime.Now.Millisecond);

public string GetRandomFileFromGivenFolderPath()
{
if (FolderPath_RandomFiles.Text != " ")
{
// Get an array of all file paths in the specified folder
string[] files = Directory.GetFiles(FolderPath_RandomFiles.Text);

// If no files are found, return null
if (files.Length == 0)
{
UpdateRichTextBox("No Files found!");
return "null";
}

// Generate a random number between 0 and the number of files minus 1
int randomIndex = -1;
do
{
randomIndex = random.Next(0, files.Length);
} while (usedRandomNumbers.Contains(randomIndex));

usedRandomNumbers.Add(randomIndex);

// Return the file path at the random index
return files[randomIndex];
}
else
{
UpdateRichTextBox("Folder path for \"random Files Path\" not found!");
return "null";
}
}
Temptica
Temptica2y ago
yeah, that shoudl do just make sure to make a new list<int> if you intend to use the methode multiple times without fully restoring the app otherwise no single number woudl work if you use it a few times 😄
Meistro
MeistroOP2y ago
would usedRandomNumbers.clear(); also work every time I call it ? but well ! thank you very much guys !
Accord
Accord2y ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server