C Board  

Go Back   C Board > General Programming Boards > C# Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 05-27-2008, 12:17 PM   #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);
            }
        }
AlmightyDerek is offline   Reply With Quote
Old 05-27-2008, 11:56 PM   #2
the hat of redundancy hat
 
nvoigt's Avatar
 
Join Date: Aug 2001
Location: Hannover, Germany
Posts: 2,754
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.
nvoigt is offline   Reply With Quote
Reply

Tags
array, color, random

Thread Tools
Display Modes

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Help with Searching Array for Longest Sequence of an Element w2look C Programming 7 11-25-2008 01:50 AM
1-D array jack999 C++ Programming 24 05-12-2006 07:01 PM
Template Array Class hpy_gilmore8 C++ Programming 15 04-11-2004 11:15 PM
sorting a randomly generated dynamic array Led Zeppelin C Programming 4 05-03-2002 09:10 AM
Hi, could someone help me with arrays? goodn C Programming 20 10-18-2001 09:48 AM


All times are GMT -6. The time now is 01:35 AM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0 RC2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22