Pick Random String From Array

2019-01-11 01:36发布

问题:

How do I go about picking a random string from my array but not picking the same one twice.

string[] names = { "image1.png", "image2.png", "image3.png", "image4.png", "image5.png" };

Is this possible? I was thinking about using

return strings[random.Next(strings.Length)];

But this has the possibility of returning the same string twice. Or am I wrong about this? Should I be using something else like a List to accomplish this. Any feedback is welcome.

回答1:

The simplest way (but slow for large lists) would be to use a resizeable container like List and remove an element after picking it. Like:

var names = new List<string> { "image1.png", "image2.png", "image3.png", "image4.png", "image5.png" };

int index = random.Next(names.Count);
var name = names[index];
names.RemoveAt(index);
return name;

When your list is empty, all values were picked.

A faster way (especially if your list is long) would be to use a shuffling algorithm on your list. You can then pop the values out one at a time. It would be faster because removing from the end of a List is generally much faster than removing from the middle. As for shuffling, you can take a look at this question for more details.



回答2:

Try this code below

string[] Titles = { "Excellent", "Good", "Super", "REALLY GOOD DOCTOR!", "THANK YOU!", "THE BEST", "EXCELLENT PHYSICIAN", "EXCELLENT DOCTOR" };

comments_title.Value=Titles[new Random().Next(0,Titles.Length) ] ;


回答3:

You can shuffle the array in a first step, and then simply iterate over the shuffled array.
This has the advantage of being O(n) compared to O(n^2) the RemoveAt based implementations have. Of course this doesn't matter much for short arrays.

Check Jon Skeet's answer to the following question for a good(all orders are equally likely) implementation of shuffe: Is using Random and OrderBy a good shuffle algorithm?



回答4:

The best thing to do is just create a duplicate list, then as you randomly pick out a string, you can remove it from the duplicate list so that you can't pick it twice.



回答5:

The logic you could use is as follows:

1) Pick a random integer over the range equal to the length of your array. You can do this using the System.Random class.

2) Use the string corresponding to that array index

3) Delete the item with that index from the array (may be easier with a list)

Then you can pick again and the same string won't appear. The array will be one element shorter.



回答6:

You would need to keep track of the ones you have used, preferably in a List if you don't want/can't to modify the original array. Use a while loop to check that it hasn't been used, and then add it to the "used" list.



回答7:

//SET LOWERLIMIT
cmd = new SqlCommand("select min(sysid) as lowerlimit from users", cs);
int _lowerlimit = (int) cmd.ExecuteScalar();
lowerlimit = _lowerlimit;

//SET UPPERLIMIT
cmd = new SqlCommand("select max(sysid) as upperlimit from users", cs);
int _upperlimit = (int) cmd.ExecuteScalar();
upperlimit = _upperlimit;

//GENERATE RANDOM NUMBER FROM LOWERLIMIT TO UPPERLIMIT
Random rnd = new Random();
int randomNumber = rnd.Next(lowerlimit, upperlimit+1);

//DISPLAY OUTPUT
txt_output.Text += randomNumber;


回答8:

Use the below utility method

public static class ListExtensions
{
    public static T PickRandom<T>(this List<T> enumerable)
    {
        int index = new Random().Next(0, enumerable.Count());
        return enumerable[index];
    }
}

Then call the below way

string[] fruitsArray = { "apple", "orange"};
string inputString = fruitsArray.ToList().PickRandom();