Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7958147
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T04:22:34+00:00 2026-06-04T04:22:34+00:00

I’ve got a vey simple form with a grid of 16 labels, each of

  • 0

I’ve got a vey simple form with a grid of 16 labels, each of which I wish to set the Background color at random from 12 colors I have chosen.

Here is my code:

private void btnRandom_Click(object sender, EventArgs e)
    {
        txtA1.BackColor = RandomColor();
        txtA2.BackColor = RandomColor();
        txtA3.BackColor = RandomColor();
        txtA4.BackColor = RandomColor();
        txtB1.BackColor = RandomColor();
        txtB2.BackColor = RandomColor();
        txtB3.BackColor = RandomColor();
        txtB4.BackColor = RandomColor();
        txtC1.BackColor = RandomColor();
        txtC2.BackColor = RandomColor();
        txtC3.BackColor = RandomColor();
        txtC4.BackColor = RandomColor();
        txtD1.BackColor = RandomColor();
        txtD2.BackColor = RandomColor();
        txtD3.BackColor = RandomColor();
        txtD4.BackColor = RandomColor();
    }

    private Color RandomColor()
    {            
        Random rand = new Random();
        int r = rand.Next(1, 12);
        switch (r)
        {
            case 1:
                return Color.FromKnownColor(KnownColor.DodgerBlue);
            case 2:
                return Color.FromKnownColor(KnownColor.MediumAquamarine);
            case 3:
                return Color.FromKnownColor(KnownColor.Teal);
            case 4:
                return Color.FromKnownColor(KnownColor.OrangeRed);
            case 5:
                return Color.FromKnownColor(KnownColor.LightCoral);
            case 6:
                return Color.FromKnownColor(KnownColor.Red);
            case 7:
                return Color.FromKnownColor(KnownColor.MediumOrchid);
            case 8:
                return Color.FromKnownColor(KnownColor.MediumPurple);
            case 9:
                return Color.FromKnownColor(KnownColor.DarkOrchid);
            case 10:
                return Color.FromKnownColor(KnownColor.Lime);
            case 11:
                return Color.FromKnownColor(KnownColor.PaleGreen);
            case 12:
                return Color.FromKnownColor(KnownColor.SeaGreen);
            default:
                return Color.FromKnownColor(KnownColor.White);
        }
    }

But what is happening is that ALL 16 labels are assigned the same backcolor, rather than each being randomized individually.

What am I doing wrong?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T04:22:35+00:00Added an answer on June 4, 2026 at 4:22 am

    Probable cause of the issue

    It because you are creating Random object each time the RandomColor() method is called and call it few times almost at the same time.

    Explanation

    When you write new Random(); the pseudorandom function is initiated with a seed based on a current time. Seed is a value that determines the values returned by the Random object (values of pseudorandom function). This means that when you create two Random objects with the same seed, they return the same values in the subsequent Next() method.

    In your case you are calling the method few times, so the seed may be the same and that’s why it returns the same value for all the labels.

    Suggested solution

    In order to fix it, move Random rand = new Random(); outside the method, to be created only once for a class instance.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a text area in my form which accepts all possible characters from
I have just tried to save a simple *.rtf file with some websites and
link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
I am trying to understand how to use SyndicationItem to display feed which is
I used javascript for loading a picture on my website depending on which small
Basically, what I'm trying to create is a page of div tags, each has
I've got a string that has curly quotes in it. I'd like to replace
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I am doing a simple coin flipping experiment for class that involves flipping a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.