|
-
May 14th, 2005, 12:35 AM
#1
Thread Starter
Lively Member
Random Numbers.
Hey, I got New Thread working 
OK, now I got this code:
VB Code:
lblAddNo1.Text = Int(Rnd(9) * 100)
Now the problem is, that everytime I open the form, the first numbers to appear are always the same, everytime the form is generated. How can I make it so that the numbers generated are TRULY random everytime the foprm is opened?
I never know what to put in this section...

So sue me... ... ... I'm just kidding...
www.fat-pie.com Flash Movies... You gotta see 'em to believe 'em!
-
May 14th, 2005, 12:41 AM
#2
Re: Random Numbers.
VB Code:
Dim objRandom As New System.Random( _
CType(System.DateTime.Now.Ticks Mod System.Int32.MaxValue, Integer))
Public Function GetRandomNumber( _
Optional ByVal Low As Integer = 1, _
Optional ByVal High As Integer = 100) As Integer
' Returns a random number,
' between the optional Low and High parameters
Return objRandom.Next(Low, High + 1)
End Function
-
May 14th, 2005, 02:38 AM
#3
Thread Starter
Lively Member
-
May 14th, 2005, 02:50 AM
#4
Re: Random Numbers.
Ehm...you just need to place a method call to GetRandomNumber() for each label...
-
May 14th, 2005, 03:03 AM
#5
Thread Starter
Lively Member
Re: Random Numbers.
I did that, but when it generates a random number, each label would get the same number assigned to it as long as my set range was the same for each label. Now I'm using this code using VB.NET's Random() function:
VB Code:
Dim RandomNo As Random = New Random
'Then I put this on the label:
lblWhatever.Text = RandomNo.Next(100, 999)
This does the job great.
I never know what to put in this section...

So sue me... ... ... I'm just kidding...
www.fat-pie.com Flash Movies... You gotta see 'em to believe 'em!
-
May 14th, 2005, 10:41 AM
#6
Re: Random Numbers.
I can't understand the difference between what you did and what mendhak suggested. BTW, I assume that mendhak's code should be placed in a shared class or in the form's code.
-
May 14th, 2005, 12:12 PM
#7
Re: Random Numbers.
I can't understand what Mendhak was talking about on this one. He goes to the trouble of explicitly initializing the random object with the time tick. According to my reading of the documentation, that is the default behavior if the object is created without any arguments.
GetRandomNumber is a thin wrapper of the Random.Next method. It does make a bit more sense (since both bounds are treated the same way), but adds overhead.
Is this some thread safe concept at work?
My usual boring signature: Nothing
 
-
May 14th, 2005, 12:19 PM
#8
Fanatic Member
Re: Random Numbers.
Am I to understand from some of these answers that Randomize() is legacy. Because thats what I still use to reseed.
-
May 14th, 2005, 01:17 PM
#9
Re: Random Numbers.
Yes, it is legacy. And that is a good thing. Randomize worked fine, but there were some real oddities that you could run into if you were not careful. I made the mistake of putting it in a loop one time, and got identical series of numbers over and over again until the timer incremented so that the seed became different.
Also, you must know the joy of figuring out how to get a random number between x and y. Probably have it memorized. Well, now you can forget it. The random object has functions to do things like that.
My usual boring signature: Nothing
 
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
|