This question already has an answer here:
- Randomize a List<T> in C# 16 answers
I have an array of strings that I want to randomise. I want three random strings, that aren't the same, but everything I find online is either for numbers or in another language. At the moment my strings are being randomised, but I sometimes get the same strings, so for example: "Behind a tree, behind a tree, behind a tree".
So it should be like randomHiding1 != randomHiding 2 && randomHiding3; (I know that's not "real code", but just so you get what I mean)
This is my first post here, so I hope this is ok to ask because I haven't been able to find anyone asking a question about randomising "sentences-strings", and not just "abcdefg...." or numbers. Here's my code. Thanks in advance!
Random random = new Random();
// strings with hiding spots
string[] hidingSpot = "in a ditch|up in a tree|behind a stone|in a hole in the ground|behind a tree|in the shadows".Split('|');
int hidingChoice1 = random.Next(hidingSpot.Length);
int hidingChoice2 = random.Next(hidingSpot.Length);
int hidingChoice3 = random.Next(hidingSpot.Length);
string randomHiding1 = hidingSpot[hidingChoice1];
string randomHiding2 = hidingSpot[hidingChoice2];
string randomHiding3 = hidingSpot[hidingChoice3];
BTW, I know this code is very bad, and unnecessarily long, but I'm still fairly new to arrays and lists and so my number 1 priority is to get code that works, not code that is short etc. So I don't need tips about reading the documentation, because I am doing that constantly, but I just have a hard time remember things atm due to personal reasons.. I hope that makes sense.
Aucun commentaire:
Enregistrer un commentaire