0

I have made a simple number generator, and I have a question: is it possible for the generator to eject "red", "blue", "green", " yellow" and "white" instead of the numbers 1-5?

namespace zufallsgenerator
{
    public partial class Form1 : Form
    {
        Random r = new Random();

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void btnWhlie_Click(object sender, EventArgs e)
        {
            int summe = 0, z; 

            lblAnzeige.Text = " ";

           while (summe <= 0)
           {
                z = r.Next(1, 6);
                summe = summe + z;
           }
           lblAnzeige.Text += summe + "\n";               
        }
    }
}
2
  • It would be easy enough for you to convert it. When you say you want the generator to eject the color names... how exactly do you want to use the color names? right now you are summing the numbers and that wouldn't make sense with colors. Commented Oct 22, 2013 at 14:34
  • I have to write a program, were you can click on 5 buttons in 5 colors, and on thedsisplay you see "red" written in blue for example and so you have to press the red button... Commented Oct 22, 2013 at 14:47

6 Answers 6

11

You could create a simple array and access it with a randomly generated index, e.g.:

var r = new Random();
string[] colors = {"red", "blue", "green", "yellow", "white"};
var random_color = colors[r.Next(colors.Length)];
Sign up to request clarification or add additional context in comments.

2 Comments

Can you post the whole code? because it doesn't work for me :( but i think i have make the mistake and your code is right ;)
@Franz you need to seed your random :)
3

You could use an enum.

Define something like

enum Color
{
   Red,
   Green,
   Blue
}

Then you can cast your int to this:

Color color = (Color)r.Next(1, 6)

And, if you wish

Text = color.ToString();

3 Comments

This will fail if the generated number is > 2.
Don't use an enum for this. It means that you can never use a string which is not a valid C# identifier. If you do use an enum, you should use the [Description] attribute to get around this.
You're right on this. I originally thought that he was really interested to get a random color, not to get the text of the random colors.
2

If you get output like 1-5 you could create string[] containing 5 elements

string[] colors = new string[] { "red", "blue", "green", " yellow", "white" }

And instead of retrieving r.Next( 1, 6 ) you could retrieve colors[ r.Next( 0, 5 ) ] (because string array is 0-indexed, changed the min and max value).

1 Comment

there i get an error: "Error 1 An implicit conversion from type 'string' to 'int' is not possible." what did i make wrong? maybe you can post the whole code? I'm new in c# :(
1

Taking Xi Huan's answer, you can make it a bit prettier using an extension method:

public static T Next<T>(this System.Random Random, params T[] List)
{
    if(List.Length==0)
        return default(T);
    return List[Random.Next(0, List.Length)];
}

Then calling it is just:

var r=new System.Random();
var randon_color = r.Next("red", "blue", "green", "yellow", "white");

Comments

0

What I would do is create a list of strings. Then put the random number as i. So like List<string> list1 = new List<string>{"red", "blue", "green"}; then use your random number like this to call a random element of it. list1[randomNumber];

Comments

0

Define an array with the posible desired output values and instead of concatenating summe to text, add YourArrayOfNamedValues[summe]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.