Results 1 to 19 of 19

Thread: Ways to have a random colors in VB.Net?

  1. #1

    Thread Starter
    Hyperactive Member vincentg's Avatar
    Join Date
    Jun 2005
    Location
    Chicago IL, USA
    Posts
    261

    Ways to have a random colors in VB.Net?

    I have already some code which change the control's forecolor randomly in the timer event object which i create an array of colors

    VB Code:
    1. Private Sub tmrClock_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tmrClock.Tick
    2.         Dim MyColor(5) As Color
    3.        
    4.         MyColor(1) = Color.Azure
    5.         MyColor(2) = Color.Bisque
    6.         MyColor(3) = Color.Blue
    7.         MyColor(4) = Color.DarkKhaki
    8.         MyColor(5) = Color.AliceBlue
    9.         Me.lbl1.ForeColor = MyColor(CInt(Int((5 * Rnd()) + 1)))
    10.     End Sub


    Are there more better idea? I dont the array style here. Thanks.

  2. #2
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,104

    Re: Ways to have a random colors in VB.Net?

    Yeah, use the fromARGB() function. With this, you can designate the Red, Green, and Blue (you probably want to ignore the alpha) components directly.

    Thus:
    VB Code:
    1. dim rnd1 as new random
    2. dim mColor as color
    3.  
    4. mColor = mColor.FromARGB(rnd1.Next(0,256),rnd1.next(0,256),rnd1.next(0,256))

    That is off the top of my head, and may have some slight error to it, but it will create a random color.
    My usual boring signature: Nothing

  3. #3
    PowerPoster
    Join Date
    Aug 2005
    Location
    College Station, TX
    Posts
    4,521

    Re: Ways to have a random colors in VB.Net?

    Also might wanna try to put the "Randomize()" before your Rnd() function... in that code, the same "random" pattern of colors would pull up every time you stop and restart your app, because it is using a constant for the seed value... and that constant doesn't change. Putting in the "Randomize()" statement before changes the seed value also in accordance of your system time (i think), so the pattern won't be repeated...

    Test it out before hand.. start the app.. start the timer.. and notice the first couple of colors... then stop the app.. restart it.. and check the colors again.. are they the same "random" colors?? if so, add the "Randomize()" statment before the line...

  4. #4
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,104

    Re: Ways to have a random colors in VB.Net?

    Dude, that's VB6 talk there. Randomize is no longer in .NET.

    Look into the new Random class.
    My usual boring signature: Nothing

  5. #5
    Lively Member
    Join Date
    Jun 2005
    Posts
    76

    Re: Ways to have a random colors in VB.Net?

    if randomize isnt used, you need to do something to the seed.

    ive noticed that if i have random number generating on a forms timer event that ticks in multiples of 1 sec, they are certainly not random.

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Ways to have a random colors in VB.Net?

    You can use Randomize and Rnd, but they are part of the Microsoft.VisualBasic namespace and are there for compatibility. As I always say, there is nothing wrong with using Runtime functions but it is prefereable to use System-based alternatives if they exist, and in this case that means the Random class. If you do not supply a seed to the Random constructor it automatically uses a time-based seed, so I believe that:
    VB Code:
    1. Dim myRandom As New Random
    is equivalent to:
    VB Code:
    1. Dim myRandom As New Random(CInt(Date.Now.Ticks Mod Integer.MaxValue))
    If that's not random enough for you, Phill64 has made a submission to the CodeBank that doesn't use a psuedo-random sequence as both the Random and Randomize/Rnd methods do.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  7. #7
    PowerPoster
    Join Date
    Aug 2005
    Location
    College Station, TX
    Posts
    4,521

    Re: Ways to have a random colors in VB.Net?

    Quote Originally Posted by Shaggy Hiker
    Dude, that's VB6 talk there. Randomize is no longer in .NET.

    Look into the new Random class.
    In his code, a simple Randomize() statment above the line that included the Rnd function would've worked fine, which is what I was referring to....

  8. #8
    type Woss is new Grumpy; wossname's Avatar
    Join Date
    Aug 2002
    Location
    #!/bin/bash
    Posts
    5,682

    Re: Ways to have a random colors in VB.Net?

    "Working fine" and "good code" are 2 different things.

    Don't use legacy VB6 functions.

    You don't need to use randomize and you don't need to manually seed the Random object because Random's constructor automatically seeds itself from the system timer.

    Period.
    I don't live here any more.

  9. #9
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,104

    Re: Ways to have a random colors in VB.Net?

    Thanks' Woss ol' cat. That was my point.

    Rnd isn't just VB6 legacy in this case, though, it is actually inferior to using Random. Randomize and Rnd had problems if you called Randomize too often. Therefore, you had to decide where to call Randomize to get the correct behavior. If you just put it before Rnd (as I once did), and called it in a loop, it was quite possible to get several calls to Randomize/Rnd in a single second, at which point each call to Rnd would return the same value.

    Using the Random class, this has been taken care of.

    The other nice advantage of the Random class has to do with the Next function. Anybody who has spent much time in the Classic VB forum has seen plenty of requests for how to get a random number between x and y. The formula could be found in MSDN, but it was a bit opaque. Using Next is as clear as can be.
    My usual boring signature: Nothing

  10. #10
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Ways to have a random colors in VB.Net?

    As I said, I do think that using the Random class is the preferable option, but I don't think your criticism of Randomize/Rnd is valid. You're only supposed to call Randomize once in your program. Calling Randomize repeatedly is like creating new Random objects over and over. You would just create a single Random object and call Next repeatedly, which is equivalent to calling Randomize once and then Rnd repeatedly. I used to use Randomize/Rnd before I was aware of the Random class and I just called Randomize once in the Load event handler of my main form. No issues at all.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  11. #11
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,104

    Re: Ways to have a random colors in VB.Net?

    I agree, but I did manage to screw it up once in VB6.

    The thing is, Rnd will eventually repeat. I don't know how many calls you can make before the sequence repreats. Because of this, I was a bit lax about where I put the call to Randomize, and only called it just before I needed the Rnd object. This can be a mistake.

    You could probably make the same mistake with Random if you were to create the class in a sub that is called repeatedly in a tight loop. Each instance, though created local, would be created so rapidly that the call would effectively be seeded repeatedly with the same number. I haven't tested that.
    My usual boring signature: Nothing

  12. #12

    Thread Starter
    Hyperactive Member vincentg's Avatar
    Join Date
    Jun 2005
    Location
    Chicago IL, USA
    Posts
    261

    Re: Ways to have a random colors in VB.Net?

    wow. Sorry i was too buzy to reply... thanks..everybody

  13. #13
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Ways to have a random colors in VB.Net?

    Quote Originally Posted by Shaggy Hiker
    You could probably make the same mistake with Random if you were to create the class in a sub that is called repeatedly in a tight loop. Each instance, though created local, would be created so rapidly that the call would effectively be seeded repeatedly with the same number.
    Quite true. From the help topic for the Random constructor that takes an Integer argument:
    If your application requires different random number sequences, invoke this constructor repeatedly with different seed values. One way to produce a unique seed value is to make it time-dependent. For example, derive the seed value from the system clock.

    However, if your application runs on a fast computer the system clock might not have time to change between invocations of this constructor; the seed value might be the same for different instances of Random. In that case, apply an algorithm to differentiate the seed value in each invocation.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  14. #14
    Frenzied Member Phill64's Avatar
    Join Date
    Jul 2005
    Location
    Queensland, Australia
    Posts
    1,201

    Re: Ways to have a random colors in VB.Net?

    If your application requires different random number sequences, invoke this constructor repeatedly with different seed values. One way to produce a unique seed value is to make it time-dependent. For example, derive the seed value from the system clock.

    However, if your application runs on a fast computer the system clock might not have time to change between invocations of this constructor; the seed value might be the same for different instances of Random. In that case, apply an algorithm to differentiate the seed value in each invocation.
    I disagree, system clock is the same as what the random class uses in the first place, and if you tried to make your own maths algorithm you would get bad results, you need other random factors.

    see my True Random class here at the CodeBank http://www.vbforums.com/showthread.php?t=357926
    Last edited by Phill64; Sep 30th, 2005 at 05:57 PM.

  15. #15
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Ways to have a random colors in VB.Net?

    Quote Originally Posted by Phill64
    see my True Random class here at the CodeBank http://www.vbforums.com/showthread.php?t=357926
    Gratuitous self-promotion.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  16. #16
    Frenzied Member Phill64's Avatar
    Join Date
    Jul 2005
    Location
    Queensland, Australia
    Posts
    1,201

    Re: Ways to have a random colors in VB.Net?

    Quote Originally Posted by jmcilhinney
    Gratuitous self-promotion.
    *ahem*

    seld-promotion threads started by Jm recently:

    Create "Toast" Popup Notification Windows
    ZIP Compression Library in the Codebank
    Animated Window Effects
    Fading form in and out


  17. #17
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Ways to have a random colors in VB.Net?

    But those were hardly gratuitous, just unnecessary and unwarranted. Hang on, what's the definition of "gratuitous" again?
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  18. #18
    Frenzied Member Phill64's Avatar
    Join Date
    Jul 2005
    Location
    Queensland, Australia
    Posts
    1,201

    Re: Ways to have a random colors in VB.Net?

    gratuitous from dictionary.com

    Given or granted without return or recompense; unearned.
    Given or received without cost or obligation; free.
    Unnecessary or unwarranted; unjustified: gratuitous criticism.


    your second post came from this didnt it?

  19. #19
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Ways to have a random colors in VB.Net?

    Excellent dictionary that sits in your system tray: WordWeb

    Hmmm... off topic and advertising.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width