1

I am trying to generate Unique and prominently random colors in c# or vb.net. I am using below code now. But this code generating color almost same color, not big change.

How can unique kind of colors?

  Private Function GetRandomColor() As Color
        Dim RandGen As New Random()
        Return Color.FromArgb(RandGen.Next(70, 200), RandGen.Next(100, 225), 
        RandGen.Next(100, 230))
    End Function
James123
  • 11,184
  • 66
  • 189
  • 343
  • Just be a little nit-picky -- uniqueness is not a guaranteed quality of random numbers. – Austin Salonen Apr 15 '11 at 15:58
  • possible duplicate of [Random encounter not so random](http://stackoverflow.com/questions/2727538/random-encounter-not-so-random) – H H Apr 15 '11 at 16:03

3 Answers3

3

You would be best using a class that generates HSL colours, that way it is easier to control prominence. There are a few class out there

Such as...

http://www.geekymonkey.com/Programming/CSharp/RGB2HSL_HSL2RGB.htm

or

http://richnewman.wordpress.com/hslcolor-class/

Having control over the luminosity/saturation means you can get vibrant colours at any hue..

Using richnewmans class...

var c = new HSLColor(new Random(0,100),80,80);

we only set a random value in the hue and the sat and lum can be fixed at whatever looks best

David
  • 8,340
  • 7
  • 49
  • 71
2

Its more than likely because you are creating a new instance of Random everytime. Try moving the instance outside of the method, such as at class level, and see if that helps

i.e.

  Dim RandGen As New Random()

  Private Function GetRandomColor() As Color
        Return Color.FromArgb(RandGen.Next(70, 200), RandGen.Next(100, 225), 
        RandGen.Next(100, 230))
    End Function
Iain Ward
  • 9,850
  • 5
  • 34
  • 41
2

Try randomly choosing one of the RGB values to be low (0-100).

001
  • 13,291
  • 5
  • 35
  • 66