Thread: Randomly generating colors from an array.

  1. #1
    Registered User
    Join Date
    May 2008
    Posts
    1

    Question Randomly generating colors from an array.

    I have a class project. I need to fill a form with multicolored snow using an array. I've loaded 3 colors into an array, but I can't get it to randomly pick from the array because it can't convert system.drawing.color to an integer. Can anyone help me get around this? This is my code so far.

    insert
    Code:
    System.Drawing.Color[] colorList = new System.Drawing.Color[3];
    
                colorList[0] = Color.Orange;
                colorList[1] = Color.Green;
                colorList[2] = Color.Red;
    
                DateTime currentDateTime = DateTime.Now;
                Random colorRandom = new Random(currentDateTime.Millisecond);
    
                Color snowPen;
                
                snowPen =  colorList[];
                
    
    
                DateTime currentDateTime2 = DateTime.Now;
                Random snowRandom = new Random(currentDateTime.Millisecond);
    
    
                int xIntegerSnow = Convert.ToInt32(this.Width / 2),
                    yIntegerSnow = Convert.ToInt32(this.Height / 2);
                Pen whitePen = new Pen(snowPen, 2);
    
    
                // Make it snow in random locations.
    
                for (int indexInteger = 1; indexInteger < 40000; indexInteger++)
                {
                    int xInteger = snowRandom.Next(1, this.Width);
                    int yInteger = snowRandom.Next(1, this.Height);
                    e.Graphics.DrawLine(whitePen, xInteger, yInteger,
                          xInteger + 1, yInteger + 1);
                }
            }

  2. #2
    the hat of redundancy hat nvoigt's Avatar
    Join Date
    Aug 2001
    Location
    Hannover, Germany
    Posts
    3,130
    snowPen = colorList[];

    In that line, pick one color ar random:

    snowPen = colorList[snowRandom.Next(0,2)];

    Now it will pick one color at random and create the pen. You probably want to have each line in a different color, so move the color selection and pen creation into the loop.

    For performance reasons you should create an array of pens with each pen the color of one of your arrays of colors. Then in your loop select one pen randomly just like you did with the colors before.
    hth
    -nv

    She was so Blonde, she spent 20 minutes looking at the orange juice can because it said "Concentrate."

    When in doubt, read the FAQ.
    Then ask a smart question.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 7
    Last Post: 11-25-2008, 01:50 AM
  2. 1-D array
    By jack999 in forum C++ Programming
    Replies: 24
    Last Post: 05-12-2006, 07:01 PM
  3. Template Array Class
    By hpy_gilmore8 in forum C++ Programming
    Replies: 15
    Last Post: 04-11-2004, 11:15 PM
  4. sorting a randomly generated dynamic array
    By Led Zeppelin in forum C Programming
    Replies: 4
    Last Post: 05-03-2002, 09:10 AM
  5. Hi, could someone help me with arrays?
    By goodn in forum C Programming
    Replies: 20
    Last Post: 10-18-2001, 09:48 AM

Tags for this Thread