0

I'm trying to set the console background color to a random color but it always returns magenta. What would I have to change to fix this issue. Thanks!

    using System;

    namespace ConsoleApplication
{
    public class Program
    {
        public static void Main(string[] args)
        {
            Random random = new Random();
            int randomInt = random.Next(0, 6);
            while(randomInt < 7) 
            {
                Console.BackgroundColor = ConsoleColor.Red;
                randomInt++;
                Console.BackgroundColor = ConsoleColor.Blue;
                randomInt++;
                Console.BackgroundColor = ConsoleColor.Cyan;
                randomInt++;
                Console.BackgroundColor = ConsoleColor.Green;
                randomInt++;
                Console.BackgroundColor = ConsoleColor.Red;
                randomInt++;
                Console.BackgroundColor = ConsoleColor.Yellow;
                randomInt++;
                Console.BackgroundColor = ConsoleColor.Magenta;
                randomInt++;
            }
        }
    }
}
Kyle
  • 135
  • 1
  • 1
  • 8
  • 2
    use a debugger and step thru - you should see your mistake... – Daniel A. White Feb 12 '17 at 13:41
  • I don't understand the question - the last color that gets set by the `while` loop is magenta. You probably want to use a different construct, such as `if` or `switch` `case` – UnholySheep Feb 12 '17 at 13:42
  • 1
    Apparently you wanted to create some sort of a [Duff's device](https://en.wikipedia.org/wiki/Duff%27s_device) in hope that `randomInt` is reevaluated for being `< 7` after each instruction within the `while`, and that it will automatically exit the `while` when it happens. That's not how it works. – GSerg Feb 12 '17 at 13:44
  • You could try seeding your random number so that it doesn't always start out with the same sequence. See this SO post http://stackoverflow.com/questions/4060961/seeding-a-pseudo-random-number-generator-in-c-sharp – nocturns2 Feb 12 '17 at 13:55
  • Also, in your while loop, you're not giving it time to see the color change. So it goes all the way to the end and you end up with the same color. – nocturns2 Feb 12 '17 at 13:58

2 Answers2

3

You have misunderstood the concept of a loop I believe and it may not be the tool for you to use. To randomize colors, you would have to associate them with a number and then pick a number and select the associated color.

The ConsoleColor is an enumeration which means each value is already associated with a number. You can select a random value from the enumeration using the method described in this question.

If you want to specifically only have a few colors from the enumeration, you will have to create your own array of the values you want and select a value from that array.

Here is an example of how to select a random item from an array.

Random random = new Random();
ConsoleColor[] colors = new ConsoleColor[] { ConsoleColor.Red, ConsoleColor.Blue };
var myRandomColor = colors[random.Next(0, colors.Length)];
Community
  • 1
  • 1
Pontus Magnusson
  • 632
  • 1
  • 10
  • 25
0

You are actually setting all colors and last magenta. I think you should use if or case statement if you want to set colors conditionally. If you comment the magenta assignmnent you will always get yellow background

        while(randomInt < 7) 
        {
            Console.BackgroundColor = ConsoleColor.Red;
            randomInt++;
            Console.BackgroundColor = ConsoleColor.Blue;
            randomInt++;
            Console.BackgroundColor = ConsoleColor.Cyan;
            randomInt++;
            Console.BackgroundColor = ConsoleColor.Green;
            randomInt++;
            Console.BackgroundColor = ConsoleColor.Red;
            randomInt++;
            Console.BackgroundColor = ConsoleColor.Yellow;
            randomInt++;
            //Console.BackgroundColor = ConsoleColor.Magenta;
            //randomInt++; //THIS WILL ALWAYS BE YELLOW
        }

I think that what you need is a case statement:

switch(randomInt)
{
     case 0:
        Console.BackgroundColor = ConsoleColor.Red;
        break;
     case 1:
        Console.BackgroundColor = ConsoleColor.Blue;
        break;
     ///....AND SO ON
}
NicoRiff
  • 4,803
  • 3
  • 25
  • 54