locked
How to make selected colours to appear randomly? RRS feed

  • Question

  • Hi! I'm doing a colour guessing game and i do not know how appear make selected colours to appear randomly? Anyone can help me? Thanks in advance!! :D
    Friday, December 23, 2011 5:17 PM

Answers

  • Hi! I'm doing a colour guessing game and i do not know how appear make selected colours to appear randomly? Anyone can help me? Thanks in advance!! :D


    You can do this by Assigning an ID to colors(in case color is upto limited no.) and select the id using Random Class.

    in case Color isn't limited in number, u can create color using Random Function

    List<Color> col;
            private void Form1_Load(object sender, EventArgs e)
            {
                col = new List<Color>();
                load();
                Random rand = new Random();
                this.BackColor = col[rand.Next(0, 5)];
            }
            void load()
            {
                col.Add(Color.Red);
                col.Add(Color.Purple);
                col.Add(Color.Plum);
                col.Add(Color.PowderBlue);
                col.Add(Color.Peru);
                col.Add(Color.Blue);
    
            }
    private void button1_Click_1(object sender, EventArgs e)//Event for changing Color ON button click
            {
                Form1_Load(sender, e);
            }

    Revert back in case of any difficulty


    Regards Kumar Gaurav.
    Saturday, December 24, 2011 6:57 AM
  • public static class MyExtensions {
      public static T RandomItem<T>( this IList<T> me, Random random ) {
        return me[random.Next(me.Count)];
    }
    
    // Try it out like this.
    Color[] items = new Color[] {Color.Red, Color.Green, Color.Blue};
    Random random = new Random();
    Color randomColor = items.RandomItem(random);
    Color anotherRandomColor = items.RandomItem(random);
    Color yetAnotherOne = items.RandomItem(random);
    
    
    


    This is kind of a fun way to choose random things from lists or arrays.  Always supply your own random object when doing random stuff.  (A little bit of wisdom there.)

     

    Wednesday, December 28, 2011 2:16 AM

All replies

  • Check out the Random class. HTH
    William Wegerson (www.OmegaCoder.Com)
    Friday, December 23, 2011 5:21 PM
    Moderator
  • Hi! I'm doing a colour guessing game and i do not know how appear make selected colours to appear randomly? Anyone can help me? Thanks in advance!! :D


    You can do this by Assigning an ID to colors(in case color is upto limited no.) and select the id using Random Class.

    in case Color isn't limited in number, u can create color using Random Function

    List<Color> col;
            private void Form1_Load(object sender, EventArgs e)
            {
                col = new List<Color>();
                load();
                Random rand = new Random();
                this.BackColor = col[rand.Next(0, 5)];
            }
            void load()
            {
                col.Add(Color.Red);
                col.Add(Color.Purple);
                col.Add(Color.Plum);
                col.Add(Color.PowderBlue);
                col.Add(Color.Peru);
                col.Add(Color.Blue);
    
            }
    private void button1_Click_1(object sender, EventArgs e)//Event for changing Color ON button click
            {
                Form1_Load(sender, e);
            }

    Revert back in case of any difficulty


    Regards Kumar Gaurav.
    Saturday, December 24, 2011 6:57 AM
  • I have a limit of 6 colors. So should i do the same?? Thanks again!!
    Saturday, December 24, 2011 9:37 AM
  • I have a limit of 6 colors. So should i do the same?? Thanks again!!

    I guesss that would be the best option.

    Regards Kumar Gaurav.
    Saturday, December 24, 2011 1:39 PM
  • public static class MyExtensions {
      public static T RandomItem<T>( this IList<T> me, Random random ) {
        return me[random.Next(me.Count)];
    }
    
    // Try it out like this.
    Color[] items = new Color[] {Color.Red, Color.Green, Color.Blue};
    Random random = new Random();
    Color randomColor = items.RandomItem(random);
    Color anotherRandomColor = items.RandomItem(random);
    Color yetAnotherOne = items.RandomItem(random);
    
    
    


    This is kind of a fun way to choose random things from lists or arrays.  Always supply your own random object when doing random stuff.  (A little bit of wisdom there.)

     

    Wednesday, December 28, 2011 2:16 AM