-2

I have two methods. In the first one I am adding items to a list called Persons. In the second I need to select a random item from the list (Persons) and return the name of the person from the list.

I'm unsure of how to do this. I've tried generating a random number and a random letter however I can't understand how to use these with the list.

Any help would be greatly appreciated! Thank you in advance

static void PopulatePersons()
        {
            Person Bill = new Person("Bill", "no", "brown", 'm');
            Person Eric = new Person("Eric", "yes", "brown", 'm');
            Person Robert = new Person("Robert", "no", "blue", 'm');
            Person George = new Person("George", "yes", "brown", 'm');
            Person Herman = new Person("Herman", "no", "green", 'm');
            Person Anita = new Person("Anita", "no", "blue", 'f');
            Person Maria = new Person("Maria", "yes", "green", 'f');
            Person Susan = new Person("Susan", "no", "brown", 'f');
            Person Claire = new Person("Claire", "yes", "brown", 'f');
            Person Anne = new Person("Anne", "no", "brown", 'f');

            Persons = new List<Person>() 
            { Bill, Eric, Robert, George, Herman, Anita, Maria, Susan, Claire, Anne };            
        }

        static Person GetRandomPerson()
        {
            PopulatePersons();
        }
Student
  • 124
  • 12
  • https://learn.microsoft.com/pt-br/dotnet/api/system.random?view=net-5.0 just create a Randon with intervalo set to 0... List.Count and use as index list[randomGenerated] – MestreDosMagros Dec 29 '20 at 00:31
  • I've tried this however it keeps returning SolutionName.Person – Student Dec 29 '20 at 00:32

2 Answers2

2

You can use the Random class to generate a random integer within the range of the size of your list, here is an example:

public class Program
{
    public static void Main()
    {
        var persons = PopulatePersons();
        var random = new Random();
        var randomPeople = persons.ElementAt(random.Next(0, persons.Count));
        Console.WriteLine(randomPeople.Name);
    }
    
    public static List<Person> PopulatePersons()
        {
            Person Bill = new Person("Bill");
            Person Eric = new Person("Eric");
            Person Robert = new Person("Robert");
            Person George = new Person("George");
            Person Herman = new Person("Herman");
            Person Anita = new Person("Anita");
            return new List<Person>() { Bill, Eric, Robert, George, Herman, Anita };            
        }
    
    public class Person 
    {
        public Person(string name)
        {
            Name = name;
        }
        
        public string Name {get;set;}
    }
}
MestreDosMagros
  • 1,000
  • 5
  • 19
2

What you are looking for is:

 var person = PopulatePersons.Persons[yourRandomNumber];

So you method should look like this:

 static Person GetRandomPerson()
    {
      Random rndPerson= new Random();
      int rndNumber = rnd.Next(0, PopulatePersons.Pesrons.Count);
       return PopulatePersons.Persons[rndNumber];
    }
MestreDosMagros
  • 1,000
  • 5
  • 19
David B
  • 121
  • 6
  • The count - 1 is not necessary. The parameter maxValue is exclusive, so it is not taken into account: `rnd.Next(0, PopulatePersons.Pesrons.Count);` – Saber Dec 29 '20 at 01:00
  • According to this doc https://learn.microsoft.com/en-us/dotnet/api/system.random.next?view=net-5.0 Random.Next() witll return a 32-bit signed integer that is greater than or equal to 0 and less than MaxValue. So the - 1 on the List.Count is wrong, as the Next() method is already taking only values less than MaxValue parameter – MestreDosMagros Dec 29 '20 at 01:01
  • 1
    Your `Random` instance should be `static` at class level, not a local. You can also just use `return Persons[rnd.Next(Persons.Count)];` – Idle_Mind Dec 29 '20 at 01:28