-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();
        }
2

2 Answers 2

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;}
    }
}
Sign up to request clarification or add additional context in comments.

Comments

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];
    }

3 Comments

The count - 1 is not necessary. The parameter maxValue is exclusive, so it is not taken into account: rnd.Next(0, PopulatePersons.Pesrons.Count);
According to this doc learn.microsoft.com/en-us/dotnet/api/… 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
Your Random instance should be static at class level, not a local. You can also just use return Persons[rnd.Next(Persons.Count)];

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.