|
-
Sep 29th, 2005, 02:13 PM
#1
Thread Starter
Hyperactive Member
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:
Private Sub tmrClock_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tmrClock.Tick
Dim MyColor(5) As Color
MyColor(1) = Color.Azure
MyColor(2) = Color.Bisque
MyColor(3) = Color.Blue
MyColor(4) = Color.DarkKhaki
MyColor(5) = Color.AliceBlue
Me.lbl1.ForeColor = MyColor(CInt(Int((5 * Rnd()) + 1)))
End Sub
Are there more better idea? I dont the array style here. Thanks.
-
Sep 29th, 2005, 02:21 PM
#2
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:
dim rnd1 as new random
dim mColor as color
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
 
-
Sep 29th, 2005, 03:31 PM
#3
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...
-
Sep 29th, 2005, 04:52 PM
#4
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
 
-
Sep 29th, 2005, 05:32 PM
#5
Lively Member
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.
-
Sep 29th, 2005, 06:39 PM
#6
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:
Dim myRandom As New Random
is equivalent to:
VB Code:
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.
-
Sep 29th, 2005, 08:09 PM
#7
Re: Ways to have a random colors in VB.Net?
 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....
-
Sep 30th, 2005, 03:45 AM
#8
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.
-
Sep 30th, 2005, 09:45 AM
#9
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
 
-
Sep 30th, 2005, 09:52 AM
#10
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.
-
Sep 30th, 2005, 11:12 AM
#11
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
 
-
Sep 30th, 2005, 01:01 PM
#12
Thread Starter
Hyperactive Member
Re: Ways to have a random colors in VB.Net?
wow. Sorry i was too buzy to reply... thanks..everybody
-
Sep 30th, 2005, 05:22 PM
#13
Re: Ways to have a random colors in VB.Net?
 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.
-
Sep 30th, 2005, 05:52 PM
#14
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.
-
Sep 30th, 2005, 06:07 PM
#15
Re: Ways to have a random colors in VB.Net?
 Originally Posted by Phill64
Gratuitous self-promotion.
-
Sep 30th, 2005, 06:15 PM
#16
Re: Ways to have a random colors in VB.Net?
 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
-
Sep 30th, 2005, 06:22 PM
#17
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?
-
Sep 30th, 2005, 06:26 PM
#18
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?
-
Sep 30th, 2005, 06:41 PM
#19
Re: Ways to have a random colors in VB.Net?
Excellent dictionary that sits in your system tray: WordWeb
Hmmm... off topic and advertising.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|