generating random background colors RRS feed

  • Question

  • User1519602350 posted

    I have more than 20 boxes. I would like to generate each box should have different background color(below highlighted and bold). How to generate in for each loop. below is my code.

     @foreach(var subMenuCategories in subcategory)
                    <div class="portfolio-overlay" style="background:#000 [looking here to generate automatic background color]">                


    Monday, September 22, 2014 8:56 AM


  • User-821857111 posted

    You need to think clearly about what you want and describe it in more detail. First of all, there is no such thing as a random colour. I suspect you mean you want to choose a colour at random from a predefined list of colours. I also suspect you don't want the selection to be entirely random as that may lead to the same colour being selected in successive calls. I nteh meantime, here's how you can randomly select a colour from a collection of them:

    string[] colors = {"Red", "Green", "Blue", "Black", "Orange", "Purple", "Grey"};
    var rnd = new Random();
    var randomColor = colors.OrderBy(x => rnd.Next()).Take(1);

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, September 22, 2014 9:40 AM