|
-
Jun 19th, 2007, 07:37 AM
#1
Thread Starter
Hyperactive Member
[RESOLVED] The Best Way To Randomize?
Hello, I just have a quick question on the best way to randomize the VB random number generator. When the application starts I call these two lines only once. Is that the best way?
Code:
Call Rnd(-1) 'KB Article KB120587
Call Randomize
I call Rnd -1 before I call Randomize because of this KB article
-
Jun 19th, 2007, 07:51 AM
#2
Re: The Best Way To Randomize?
-
Jun 19th, 2007, 08:03 AM
#3
Re: The Best Way To Randomize?
Are you using EXCEL???
That article is about...
APPLIES TO
• Microsoft Excel 95 Standard Edition
• Microsoft Excel 5.0c
-
Jun 19th, 2007, 08:57 AM
#4
Re: The Best Way To Randomize?
 Originally Posted by Hassan Basri
Code:
Call Rnd(-1) 'KB Article KB120587
Call Randomize
There is no need to reseed Rnd this way.
Rnd returns from a fixed and perfectly even distributed set of 16777216 (2^27) values. The sequence always runs in the same order, Calling Rnd with a negative value and calling Randomize both just change the current position in that sequence.
Calling Rnd with a negative number reseeds the generator with the passed number, Rnd(-1) always equals 0.224007, Rnd(-2) always equals 0.7133257.
Calling Randomize Number reseeds the generator using the passed number and it's current position in the sequence. So without prior reseeding it will produce a different sequence even if called with the same number each time.
Calling Randomize on its own uses the system timer as the number, this is not quite the same as calling Randomize Timer but it is very similar (I think it's better to call Randomize on it's own).
The KB article shows how you can achieve the same sequence of numbers again and again by calling Rnd(-number1) followed by Randomize number2. In other words calling Rnd(-1) followed by Randomize is actually pretty pointless as you reseed then reseed again based on the timer. The part of the article I don't understand is if I want to repeat a sequence why not simply call Rnd(-number) on its own?
If anyone is interested calling Rnd(-16184694) will reset the sequence to it's initial state.
Last edited by Milk; Jun 19th, 2007 at 10:21 AM.
Reason: typo
-
Jun 19th, 2007, 09:07 AM
#5
Re: The Best Way To Randomize?
 Originally Posted by Milk
The KB article shows how you can achieve the same sequence of numbers again and again by calling Rnd(-number1) followed by Randomize number2.
The KB article is about a bug with VBA being used from EXCEL.
It has absolutely nothing to do with VB6 and should not be confused by future thread readers to have anything to do with VB6.
In Microsoft Excel, when you use the Randomize statement in a Visual Basic procedure to initialize the random-number generator, the Rnd function returns a different series of random numbers each time you use it in the procedure, even if you use the Randomize statement with the same number value before each Rnd function
-
Jun 19th, 2007, 09:38 AM
#6
Re: The Best Way To Randomize?
Actually it's about an error in the VBA Help documentation which implies that Randomize n resets the seed, the article goes on to say
The current functionality of the Randomize statement in Visual Basic, Applications Edition, is the same as in the Basic, Visual Basic, and QuickBasic languages. The Randomize statement does not reset the seed for the Rnd function.
and then shows you how to reset the seed.
-
Jun 19th, 2007, 11:20 AM
#7
Thread Starter
Hyperactive Member
Re: The Best Way To Randomize?
 Originally Posted by RhinoBull
I only call that once in the application.
So the best way is to call Randomize only once? Without Rnd -1 before it? If Rnd -1 before calling Randomize is that better?
-
Jun 19th, 2007, 11:24 AM
#8
Re: The Best Way To Randomize?
The accepted practice in all BASIC's I've ever used (mainframe since the 80's through now) is to call RANDOMIZE once, setting a seed value.
Then each call to RND() is done after that.
I've seen nothing to change my mind about that.
-
Jun 19th, 2007, 11:34 AM
#9
Re: The Best Way To Randomize?
 Originally Posted by Hassan Basri
...So the best way is to call Randomize only once?
Yep and perhaps when you load your form...
-
Jun 19th, 2007, 07:52 PM
#10
Thread Starter
Hyperactive Member
Re: The Best Way To Randomize?
 Originally Posted by RhinoBull
Yep and perhaps when you load your form...
Thanks. So the Rnd -1 before randomize is not necessary then?
-
Jun 19th, 2007, 07:57 PM
#11
Thread Starter
Hyperactive Member
Re: The Best Way To Randomize?
 Originally Posted by Milk
There is no need to reseed Rnd this way.
Rnd returns from a fixed and perfectly even distributed set of 16777216 (2^27) values. The sequence always runs in the same order, Calling Rnd with a negative value and calling Randomize both just change the current position in that sequence.
Calling Rnd with a negative number reseeds the generator with the passed number, Rnd(-1) always equals 0.224007, Rnd(-2) always equals 0.7133257.
Calling Randomize Number reseeds the generator using the passed number and it's current position in the sequence. So without prior reseeding it will produce a different sequence even if called with the same number each time.
Calling Randomize on its own uses the system timer as the number, this is not quite the same as calling Randomize Timer but it is very similar (I think it's better to call Randomize on it's own).
The KB article shows how you can achieve the same sequence of numbers again and again by calling Rnd(-number1) followed by Randomize number2. In other words calling Rnd(-1) followed by Randomize is actually pretty pointless as you reseed then reseed again based on the timer. The part of the article I don't understand is if I want to repeat a sequence why not simply call Rnd(-number) on its own?
If anyone is interested calling Rnd(-16184694) will reset the sequence to it's initial state.
Thanks Milk, so basically I just need to call Randomize once in the application and that is it?
-
Jun 20th, 2007, 06:34 AM
#12
Re: The Best Way To Randomize?
I'm going to be slightly controversial here and suggest that you do not call Randomize at all.
Randomize n aledgedly uses a hash function to produce a two byte seed. No matter how clever that hash function is, two bytes can only ever contain 65536 different values. This means that Randomize n can only provide 65536 different starting points to the Sequence. Tests I and others have done appear to confirm this. (I say appear because Randomize Timer is not the same as just Randomize, but Randomize does use the system timer, I just don't know how)
Rnd(-n) seems to work differently, no hashing perhaps? It seems to provide possible entry points to the whole sequence. Some tests I've done seem to indicate calling Rnd(Some big {3 byte +} negative ever changing number) once (during load perhaps) is better than calling Randomize.
Code:
Option Explicit
Private Declare Function QueryPerformanceCounter Lib "kernel32" (lpPerformanceCount As Currency) As Long
Private Sub Form_Load()
Dim N As Currency
QueryPerformanceCounter N
Rnd -N
End Sub
I need better testing to prove all this, watch this space.
P.S. thanks to bushmobile for the link
Edit: just realized fatal flaw with using (Time-Timer) how stupid, edited out.
Last edited by Milk; Jun 20th, 2007 at 07:35 AM.
-
Jun 20th, 2007, 08:07 PM
#13
Thread Starter
Hyperactive Member
Re: The Best Way To Randomize?
So that means I don't need the Rnd -1? Will it do harm if I put it?
-
Jun 21st, 2007, 04:21 AM
#14
Re: The Best Way To Randomize?
No harm, but not much point. It won't make it any more random than it already is. Calling something like Rnd(-Time) will make it more random in the sense that gives you more entry points to the (set and never changing) sequence.
-
Jun 21st, 2007, 07:31 AM
#15
Re: The Best Way To Randomize?
 Originally Posted by Hassan Basri
So that means I don't need the Rnd -1?
How many times do we have to confirm that?
-
Jun 21st, 2007, 01:03 PM
#16
New Member
Re: The Best Way To Randomize?
random number
vb Code:
randomize()
document.write(Rnd())
between 1 and 99
vb Code:
randomize()
randomNumber=Int(100 * rnd())
document.write("A random number: " & randomNumber )
i got that from online.
Last edited by fastmaxxcooper; Jun 21st, 2007 at 01:09 PM.
-
Jun 21st, 2007, 02:23 PM
#17
Lively Member
Re: The Best Way To Randomize?
Wait, isn't this the actual way to get a true random number?
Code:
Private Sub Main
Call Ramdomize
Dim lRndNum As Long: lRndNum = GetRndNum(1, 100)
Call Msgbox ("Random Number = " & lRndNum, vbInformation, "Number Generator")
End Sub
Public Function GetRndNum(ByVal lLowNum As Long, ByVal lHighNum As Long) As Long
GetRndNum = Int((lHighNum - lLowNum + 1) * Rnd + lLowNum)
End Function
-
Jun 21st, 2007, 10:10 PM
#18
Thread Starter
Hyperactive Member
Re: The Best Way To Randomize?
Thanks for everybody's replies.
Conclusion:
Call Randomize once in the application for the optimal randomization when calling the Rnd function.
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
|