Random File Repeat

sddd1

Member
Joined
Jul 22, 2021
Messages
14
Programming Experience
1-3
My Question is Random File will not repeat ,When I Click Rendom File will show "1:. Again I am Click Rendom File will show "2" But In Case show 1,1

Random rand = new Random();
string[] filess = Directory.GetFiles("Select Directory");
string randomFile = filess[rand.Next(filess.Length)];

I want How to Stop Repeat File Once will show 1, Next it never show 1
 
Check your definition of random. When something is random, there is a possibility of something repeating. Consider tossing a coin. Are you saying that with a random coin toss if the first time I toss the coin and I get heads, that means that the next time I toss the same coin, I will definitely get tails because heads will not repeat?

I think the phrase you are looking for is a "random file shuffle", not "random file".
 
Check your definition of random. When something is random, there is a possibility of something repeating. Consider tossing a coin. Are you saying that with a random coin toss if the first time I toss the coin and I get heads, that means that the next time I toss the same coin, I will definitely get tails because heads will not repeat?

I think the phrase you are looking for is a "random file shuffle", not "random file".
Can you give me any idea, If I have directory and inside 5images, I will open by random, When Click on button 1image will show, next click on button 2image will show, But incase I want don't repeat my images, I want serially one by one Like 1image, 2Images, 3images, 4images, 5images etc, But I want never repeat this images file
 
There's not really any need to implement your own shuffling code unless you need a high degree of randomness. Here are some simple shuffling options using the Random class, which is adequate for most situations:
Creating new array:
var rng = new Random();

var shuffledArray = originalArray.OrderBy(e => rng.NextDouble()).ToArray();
Shuffling in place:
var rng = new Random();
var keys = originalArray.Select(e => rng.NextDouble()).ToArray();

Array.Sort(keys, originalArray);
Once you have a shuffled array, you can just access the elements in order to get them randomised without repeating. You could use a loop over the array, be it for or foreach or you could create a Stack or Queue and then Pop or Dequeue the next item each time you need it.
 
Note that, if you were going to use a loop, you can actually use the first code without creating a new array:
C#:
var rng = new Random();

foreach (var item in originalArray.OrderBy(e => rng.NextDouble()))
{
    // Use item here.
}
Also, while this should go without saying, you should generally not be creating new Random objects locally. Create a single instance at the class level and use that each time it's required.
 
rng = new Random();

Note that, if you were going to use a loop, you can actually use the first code without creating a new array:
C#:
var rng = new Random();

foreach (var item in originalArray.OrderBy(e => rng.NextDouble()))
{
    // Use item here.
}
Also, while this should go without saying, you should generally not be creating new Random objects locally. Create a single instance at the class level and use that each time it's required.
originalArray can't find how to implement
 
Read the words. The variable originalArray is intended to refer to the original array, i.e. the array before shuffling. What's your original array? In the code you posted, it's files. You need to learn how to use examples. They are supposed to be general cases that you can modify to your specific needs. They won't necessarily be code that you can copy and paste without any thought.
 
Back
Top Bottom