C# - Add number to the end of list for each item

Using C#, in the example below, how can I add a number to the end of a list for each of the items?

For example, a random number should be added to the list of Lucky Numbers for each Cat.

using System;
using System.Linq;
using cAlgo.API;
using System.Collections.Generic;

namespace cAlgo.Robots
{
    [Robot()]
    public class DemoExample : Robot
    {
        List<Cat> myCats = new List<Cat> 
        {
            new Cat 
            {
                Name = "Sylvester",
                Age = 8,
                LuckyNumbers = {1,2,3}
            },
            new Cat 
            {
                Name = "Whiskers",
                Age = 2,
                LuckyNumbers = {4,5,6}

            },
            new Cat 
            {
                Name = "Sasha",
                Age = 13,
                LuckyNumbers = {7,8,9}
            }
        };

        protected override void OnTick()
        {

                Print("---------------");
                Random rnd = new Random();
                foreach(Cat theCat in myCats)
                {
                  Print("The average of all the lucky numbers for {0} is : {1} ", theCat.Name, theCat.LuckyNumbers.Average());
                  // Add a luck number for the cat here -- Math.Round(rnd.NextDouble() * 50)  

                }

        }

    }
}

public class Cat
{
    public string Name { get; set; }
    public int Age { get; set; }
    public List<double> LuckyNumbers = new List<double>();
}

Open in new window

LVL 10
skijAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Ken ButtersCommented:
add the following line:

theCat.LuckyNumbers.add(Math.Round(rnd.NextDouble() * 50));

Open in new window

0
Fernando SotoRetiredCommented:
This should work

protected override void OnTick( )
{
        Console.WriteLine("---------------");
        Random rnd = new Random();
        foreach(Cat theCat in myCats)
        {
          Console.WriteLine("The average of all the lucky numbers for {0} is : {1} ", theCat.Name, theCat.LuckyNumbers.Average());
          // Add a luck number for the cat here -- Math.Round(rnd.NextDouble() * 50)  
          int newNum = rnd.Next();
          theCat.LuckyNumbers.Add(newNum);
        }
}

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
skijAuthor Commented:
Thank you both, I am using parts from both your ideas.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.