Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. C# Using random Function

C# Using random Function

Scheduled Pinned Locked Moved C#
csharplounge
7 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Offline
    P Offline
    Pintu Saw
    wrote on last edited by
    #1

    code for window form application to change background color using random function anser please Regards Pintu Kumar

    D L K 4 Replies Last reply
    0
    • P Pintu Saw

      code for window form application to change background color using random function anser please Regards Pintu Kumar

      D Offline
      D Offline
      Dave Kreskowiak
      wrote on last edited by
      #2

      Easy enough, it you just generate a random number for Red, Green and Blue values, then feed those to Color.From(int, int, int)[^] to get a Color back from it. You should know what to do with that I think. No, we're not writing your code for you.

      A guide to posting questions on CodeProject[^]
      Dave Kreskowiak

      1 Reply Last reply
      0
      • P Pintu Saw

        code for window form application to change background color using random function anser please Regards Pintu Kumar

        L Offline
        L Offline
        Lost User
        wrote on last edited by
        #3

        You already asked this in QA. Please do not cross post.

        1 Reply Last reply
        0
        • P Pintu Saw

          code for window form application to change background color using random function anser please Regards Pintu Kumar

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #4

          Generating a random colour is easy,

          Color.FromArgb(random.Next() | ~0x00FFFFFF)

          ~0x00FFFFFF is a trick to write unchecked((int)0xFF000000), and the bitwise OR with that value sets the Alpha component to 255 (ie, fully opaque). It's the "most random" way to generate a colour, that is, each of the 16777216 opaque colours is generated with equal probability. Unfortunately, most of those colours look like poo. The solution is to generate a random colour in HSV space,

          Color.FromArgb(HSV2RGB(random.NextDouble(), 1, 1))
          // 1, 1 makes saturated, bright colours

          static int HSV2RGB(double h, double s, double v)
          {
          int h_i = (int)(h * 6);
          double f = h * 6 - h_i;
          double p = v * (1 - s);
          double q = v * (1 - f * s);
          double t = v * (1 - (1 - f) * s);
          double r, g, b;
          switch (h_i)
          {
          default:
          r = v; g = t; b = p;
          break;
          case 1:
          r = q; g = v; b = p;
          break;
          case 2:
          r = p; g = v; b = t;
          break;
          case 3:
          r = p; g = q; b = v;
          break;
          case 4:
          r = t; g = p; b = v;
          break;
          case 5:
          r = v; g = p; b = q;
          break;
          }
          return (-0x01000000 | ((int)(255 * r) << 16)) | (((int)(255 * g) << 8) | (int)(255 * b));
          }

          -0x01000000 is yet an other trick to write unchecked((int)0xFF000000). When generating multiple colours, this method still sort of fails: it will produce clashing colours. To avoid that, start with a random colour and add a specific offset

          double h_colour = random.NextDouble();
          for (int i = 0; i < colours.Length; i++)
          {
          colours[i] = Color.FromArgb(HSV2RGB(h_colour, 1, 1));
          h_colour = (h_colour + 0.618033988749895) % 1; // that's the golden ratio
          }

          M 1 Reply Last reply
          0
          • P Pintu Saw

            code for window form application to change background color using random function anser please Regards Pintu Kumar

            K Offline
            K Offline
            Kevin Bewley
            wrote on last edited by
            #5

            Sounds like homework to me but here's a couple of clues:

            // Instantiate a Random number generator
            Random myRandom = new Random();

            // Get a random number between 0 and 100
            int randomNumber = myRandom.Next(101);

            Now, how could I take my randomNumber and select a colour based on that value..? Perhaps you could do a little research into system.drawing.color? Then I need to do something like the code below to change the background colour... this.BackColor = System.Drawing.Color.Red;

            1 Reply Last reply
            0
            • L Lost User

              Generating a random colour is easy,

              Color.FromArgb(random.Next() | ~0x00FFFFFF)

              ~0x00FFFFFF is a trick to write unchecked((int)0xFF000000), and the bitwise OR with that value sets the Alpha component to 255 (ie, fully opaque). It's the "most random" way to generate a colour, that is, each of the 16777216 opaque colours is generated with equal probability. Unfortunately, most of those colours look like poo. The solution is to generate a random colour in HSV space,

              Color.FromArgb(HSV2RGB(random.NextDouble(), 1, 1))
              // 1, 1 makes saturated, bright colours

              static int HSV2RGB(double h, double s, double v)
              {
              int h_i = (int)(h * 6);
              double f = h * 6 - h_i;
              double p = v * (1 - s);
              double q = v * (1 - f * s);
              double t = v * (1 - (1 - f) * s);
              double r, g, b;
              switch (h_i)
              {
              default:
              r = v; g = t; b = p;
              break;
              case 1:
              r = q; g = v; b = p;
              break;
              case 2:
              r = p; g = v; b = t;
              break;
              case 3:
              r = p; g = q; b = v;
              break;
              case 4:
              r = t; g = p; b = v;
              break;
              case 5:
              r = v; g = p; b = q;
              break;
              }
              return (-0x01000000 | ((int)(255 * r) << 16)) | (((int)(255 * g) << 8) | (int)(255 * b));
              }

              -0x01000000 is yet an other trick to write unchecked((int)0xFF000000). When generating multiple colours, this method still sort of fails: it will produce clashing colours. To avoid that, start with a random colour and add a specific offset

              double h_colour = random.NextDouble();
              for (int i = 0; i < colours.Length; i++)
              {
              colours[i] = Color.FromArgb(HSV2RGB(h_colour, 1, 1));
              h_colour = (h_colour + 0.618033988749895) % 1; // that's the golden ratio
              }

              M Offline
              M Offline
              Matt T Heffron
              wrote on last edited by
              #6

              harold aptroot wrote:

              ~0x000000FF ~0x00FFFFFF is a trick to write unchecked((int)0xFF000000)

              FTFY (~0x000000FF gives 0xFFFFFF00)

              L 1 Reply Last reply
              0
              • M Matt T Heffron

                harold aptroot wrote:

                ~0x000000FF ~0x00FFFFFF is a trick to write unchecked((int)0xFF000000)

                FTFY (~0x000000FF gives 0xFFFFFF00)

                L Offline
                L Offline
                Lost User
                wrote on last edited by
                #7

                I can actually not believe I did that. WTF. It's on the level of 1+1=3.

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups