Skip to content

Random Properties

Generate characters from arrays of properties.

An example of generating random properties about a character. The values are stored in arrays, and when an instance of the Character class is created, property values are randomly assigned. A public method, characterInformation(), writes out the properties.

Random properties

Code: Random Properties

//ProgrammingIsFun.com

using System;

namespace RandomProperties
{
    class Program
    {
        static void Main()
        {
            Console.WriteLine("You round a corner and you see ... ");
            Character person = new Character();
            person.characterInformation();
            Console.ReadKey();
        }
    }

    class Character
    {
        private string Name;
        private string EyeColor;
        private string HairColor;
        private string ObjectAdjective;
        private string Object;
        private static Random myRandom = new Random();

        public Character()
        {
            this.Name = getName();
            this.EyeColor = getEyeColor();
            this.HairColor = getHairColor();
            this.Object = getObject();
            this.ObjectAdjective = getObjectAdjective();
        }

        private string getName()
            {
                string[] nameList = new String[] { "Andy", "Abel", "Alan", "George", "Samuel", "Dan" };
                return nameList[getRandomNumber(nameList.Length)]; 
            }

        private string getEyeColor()
            {
                string[] colorList = new String[] { "blue", "green", "grey", "brown", "hazel" };
                return colorList[getRandomNumber(colorList.Length)];
            }

        private string getHairColor()
            {
                string[] hairColorList = new String[] { "blue", "green", "grey", "brown", "hazel" };
                return hairColorList[getRandomNumber(hairColorList.Length)];
            }

        private string getObject()
            {
                string[] objectList = new String[] { "orb", "hat", "spoon", "book", "widget" };
                return objectList[getRandomNumber(objectList.Length)];
            }

        private string getObjectAdjective()
            {
                string[] adjectiveList = new String[] { "shiny", "worn", "rugged", "purple", "glittery" };
                return adjectiveList[getRandomNumber(adjectiveList.Length)];
            }

        private int getRandomNumber(int range)
            {
                return myRandom.Next(range);
            }
        public void characterInformation()
        {
            Console.WriteLine(Name + ", a " + EyeColor + "-eyed, " + HairColor + " haired person holding a " + ObjectAdjective + " " + Object
                + ".");
        }
    }
}