Select Random String From An Array In .NET Core and C#

How to select a random string from an array of strings
This article demonstrates how to pick a random string an array of strings.
We can use the random number generator to pick a random item from an array. The following code snippet has an array of author names (strings). We can pick a random author by generating a random number that is less than the number of items in the array and use the random index to pick a random author name in the string.
The Random.Next method generates a random integer and by passing the maximum length of the array means the random number cannot be greater than the number of items in the array. 
  1. using System;  
  2. public class RandomStringInArraySample  
  3. {  
  4. public static void Main()  
  5. {  
  6. // A array of authors  
  7. string[] authors = { "Mahesh Chand""Jeff Prosise""Dave McCarter""Allen O'neill",  
  8. "Monica Rathbun""Henry He""Raj Kumar""Mark Prime",  
  9. "Rose Tracey""Mike Crown" };  
  11. // Create a Random object  
  12. Random rand = new Random();  
  13. // Generate a random index less than the size of the array.  
  14. int index = rand.Next(authors.Length);  
  15. // Display the result.  
  16. Console.WriteLine($"Randomly selected author is {authors[index]}");  
  18. Console.ReadKey();  
  19. }  
  20. }  
Figure 1
Figure 2
In this article and code snippet, we discussed how to select a random string fron an array of strings.