C # Select a random item from a list

I am creating a console application for a small quiz. I made a list with three questions. How can I let the program randomly select a question and print it in the console?

I tried several different codes, but apparently it somehow didn't work. This is the last code I tried that I received from another user from this site, but I get errors:

"The name 'string' does not exists in current context" 

"Since 'Quiz.Questions.main ()' returns void, the return keyword should not be accompanied by an object expression

Here is the last piece of code I tried:

 class Questions { public static void main() { var questions = new List<string>{ "question1", "question2", "question3"}; int index = Random.Next(strings.Count); questions.RemoveAt(index); return questions; } 

}

Thank you all for your answers. I fixed my problem by creating an array instead of a list. This is my code now:

 class Questions { public static void main() { string[] questions = new string[3]; questions[0] = "question1"; questions[1] = "question2"; questions[2] = "question3"; Random rnd = new Random(); Console.WriteLine(questions[rnd.Next(0,2)]); } } 
+6
source share
7 answers

Are you sure you want to delete the question and return the remaining questions? Should you not only choose one? Something like that:

 public static void main() { var random = new Random(); var questions = new List<string>{ "question1", "question2", "question3"}; int index = random.Next(questions.Count); Console.WriteLine(questions[index]); } 
+11
source

You need the status of System.Console.WriteLine.

 class Questions { public static void main() { var questions = new List<string>{ "question1", "question2", "question3"}; int index = Random.Next(questions.Count); System.Console.WriteLine(questions[index]); } } 
+4
source

Try something like this

 public static void main() { var questions = new List<string>{ "question1", "question2", "question3"}; Random rnd = new Random(); int index = rnd.Next(questions.Count) string question = questions[index]; questions.RemoveAt(index); // Are you sure you neex to remove? System.Console.WriteLine(question); } 

There is a typo in which you use string instead of questions . In addition, the Random object must be initialized.

+3
source

The name "string" does not exist in the current context "

I guess you want

 int index = random.Next(questions.Count); // according to the lower-case random see last paragraph 

instead

 int index = Random.Next(strings.Count); 

Since there is no variable called strings , and you still want to delete one question.

In addition, you cannot return something from the void method. Create one that returns a list:

 private Random random = new Random(); List<string> GetRemoveQuestion(List<string> questions) { int index = random.Next(questions.Count); questions.RemoveAt(index); return questions; } 

Edit : last but not least: you cannot use Random.Next . This assumes that there is a static Next method in Random , which is not so. Therefore, I showed above how you create and use an instance. Please note that you should not create it yourself, because it is populated with the current time. If you call this method very quickly, you will get a frequent β€œrandom” value.

See msdn in the notes section for more details.

+1
source

For other search engines, the advantage is: if you need a depletion list so that you can use all items randomly, do the following:

 //use the current time to seed random so it different every time we run it Random rand = new Random(DateTime.Now.ToString().GetHashCode()); var list = new List<string>{ "item1", "item2", "item3"}; //keep extracting from the list until it depleted while (list.Count > 0) { int index = rand.Next(0, list.Count); Console.WriteLine("Rand Item: " + list[index]); list.RemoveAt(index); } 
+1
source

You have some minor bugs.

You need a reference to the Rand object. You look at strings instead of questions . You delete an item instead of selecting it.

Try the following:

 void Main() { Random rand = new Random(); var questions = new List<string>{ "question1", "question2", "question3"}; int index = rand.Next(questions.Count); return questions[index]; // If you want to use Linq then // return questions.Skip(index).Take(1); } 
0
source

Something like this might be what you want:

 private Random rng; T PickRandom<T>(List<T> list) { return list[rng.NextInt(list.Count)]; } 

You can call it in your list to get a random element from it.

0
source

Source: https://habr.com/ru/post/955765/


All Articles