Results 1 to 18 of 18

Thread: [RESOLVED] The Best Way To Randomize?

  1. #1

    Thread Starter
    Hyperactive Member Hassan Basri's Avatar
    Join Date
    Sep 2006
    Posts
    324

    Resolved [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

  2. #2

  3. #3
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    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

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  4. #4
    Cumbrian Milk's Avatar
    Join Date
    Jan 2007
    Location
    0xDEADBEEF
    Posts
    2,448

    Re: The Best Way To Randomize?

    Quote 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

  5. #5
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: The Best Way To Randomize?

    Quote 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

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  6. #6
    Cumbrian Milk's Avatar
    Join Date
    Jan 2007
    Location
    0xDEADBEEF
    Posts
    2,448

    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.

  7. #7

    Thread Starter
    Hyperactive Member Hassan Basri's Avatar
    Join Date
    Sep 2006
    Posts
    324

    Re: The Best Way To Randomize?

    Quote Originally Posted by RhinoBull
    I don't know how calling the Randomize every time you call Rnd could be best... Check the following links:

    http://www.vbforums.com/showthread.p...mber+generator
    http://www.vbforums.com/showthread.p...mber+generator
    http://www.vbforums.com/showthread.p...mber+generator
    http://www.vbforums.com/showthread.p...mber+generator

    There are many others available so as always ... use our search engine.
    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?

  8. #8
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    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.

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  9. #9

  10. #10

    Thread Starter
    Hyperactive Member Hassan Basri's Avatar
    Join Date
    Sep 2006
    Posts
    324

    Re: The Best Way To Randomize?

    Quote Originally Posted by RhinoBull
    Yep and perhaps when you load your form...
    Thanks. So the Rnd -1 before randomize is not necessary then?

  11. #11

    Thread Starter
    Hyperactive Member Hassan Basri's Avatar
    Join Date
    Sep 2006
    Posts
    324

    Re: The Best Way To Randomize?

    Quote 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?

  12. #12
    Cumbrian Milk's Avatar
    Join Date
    Jan 2007
    Location
    0xDEADBEEF
    Posts
    2,448

    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.

  13. #13

    Thread Starter
    Hyperactive Member Hassan Basri's Avatar
    Join Date
    Sep 2006
    Posts
    324

    Re: The Best Way To Randomize?

    So that means I don't need the Rnd -1? Will it do harm if I put it?

  14. #14
    Cumbrian Milk's Avatar
    Join Date
    Jan 2007
    Location
    0xDEADBEEF
    Posts
    2,448

    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.

  15. #15

  16. #16
    New Member
    Join Date
    Jun 2007
    Posts
    14

    Re: The Best Way To Randomize?

    random number

    vb Code:
    1. randomize()
    2. document.write(Rnd())

    between 1 and 99
    vb Code:
    1. randomize()
    2. randomNumber=Int(100 * rnd())
    3. document.write("A random number: " & randomNumber )

    i got that from online.
    Last edited by fastmaxxcooper; Jun 21st, 2007 at 01:09 PM.

  17. #17
    Lively Member
    Join Date
    May 2004
    Location
    Home
    Posts
    85

    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

  18. #18

    Thread Starter
    Hyperactive Member Hassan Basri's Avatar
    Join Date
    Sep 2006
    Posts
    324

    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
  •  



Click Here to Expand Forum to Full Width