dcsimg
Results 1 to 12 of 12

Thread: How exactly do you reinitialize RND in VB6?

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Oct 2008
    Posts
    961

    How exactly do you reinitialize RND in VB6?

    According to the vb6 help file. You can reinitialize RND by using a negative number, where the positive equivalent of that number is the random seed. It says:

    "If number is------------------Rnd generates"
    "Less than zero---------------The same number every time, using number as the seed."

    Now to test this, since the default initial seed for RND is 327680, I've decided to reset it to this.

    Code:
    Print Rnd
    Rnd -327680 
    Print Rnd
    Strangely enough I found that the second call to Print Rnd did NOT generate the same output as the first call to Print Rnd.
    Thinking that maybe the very act of calling "Rnd -327680" also caused it to output, I decided to test that theory using this code
    Code:
    Print Rnd
    Print Rnd(-327680)
    Again I found that the second call to Print Rnd did NOT generate the same output as the first call to Print Rnd.

    Then I turned to online sources and found I needed a Randomize statement. So I tried this
    Code:
    Print Rnd
    Rnd -1
    Randomize 327680
    Print Rnd
    This also didn't work to truly reset it to the same state as when the program starts. Neither did the next several codes I tried

    Code:
    Print Rnd
    Randomize 327680
    Rnd -1
    Print Rnd
    Code:
    Print Rnd
    Randomize 327680
    Print Rnd(-1)
    Any assistance on how to reset Rnd to the same initial state as when the program starts will be much appreciated.

  2. #2
    PowerPoster
    Join Date
    Aug 2011
    Location
    B.C., Canada
    Posts
    2,887

    Re: How exactly do you reinitialize RND in VB6?

    http://msdn.microsoft.com/en-us/libr...(v=vs.80).aspx

    Code:
    
    Note 
    
    
    
    
    To repeat sequences of random numbers, call Rnd with a negative argument immediately before using Randomize with a numeric argument. Using Randomize with the same value for Number does not repeat the previous sequence.
     
    
    
    
    
    Security Note 
    
    
    
    
    Because the Random statement and the Rnd function start with a seed value and generate numbers that fall within a finite range, the results may be predictable by someone who knows the algorithm used to generate them. Consequently, the Random statement and the Rnd function should not be used to generate random numbers for use in cryptography.

  3. #3
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    12,424

    Re: How exactly do you reinitialize RND in VB6?

    Been a while since I tested and could have changed in VB6 but in previous versions if you did not call Randomize the rnd function would always return the same sequence of numbers. Likewise if you called Randomize and gave it a fixed seed number the sequence would always be the same. In older versions you had to call Randomize Timer at the top of the code to get a random result later I think they made timer the default seed so just a call to random should give the same result as would have randomize timer.

    In VB6 the following code would always generate the same number
    Code:
    Private Sub Command1_Click()
    Rnd -5
    Randomize 5
    Debug.Print Rnd
    
    End Sub

  4. #4
    PowerPoster
    Join Date
    Aug 2011
    Location
    B.C., Canada
    Posts
    2,887

    Re: How exactly do you reinitialize RND in VB6?

    when i did use

    Code:
     Rnd(3)
    Randomize 3
    i never got the same results

    but when i used
    Code:
    Rnd(-3)
    Randomize 3
    i always got same results

    ok i did a little sample try this code

    Code:
    Private Sub Form_Load()
    Debug.Print "Results 1-A (Not Randomized)"
    Debug.Print Rnd(3), Rnd(3), Rnd(3)
    Debug.Print Rnd(3), Rnd(3), Rnd(3)
    Debug.Print "Results 1-B (With Randomized)"
    Rnd (3)
    Randomize 3
    Debug.Print Rnd(3), Rnd(3), Rnd(3)
    Rnd (3)
    Randomize 3
    Debug.Print Rnd(3), Rnd(3), Rnd(3): Debug.Print ""
    Debug.Print "Results 2-A (Not Randomized)"
    Debug.Print Rnd(3), Rnd(3), Rnd(3)
    Debug.Print Rnd(3), Rnd(3), Rnd(3)
    Debug.Print "Results 2-B (With Randomized)"
    Rnd (3)
    Randomize 3
    Debug.Print Rnd(3), Rnd(3), Rnd(3)
    Rnd (3)
    Randomize 3
    Debug.Print Rnd(3), Rnd(3), Rnd(3): Debug.Print ""
    Debug.Print "Results 3-A (Not Randomized)"
    Debug.Print Rnd(3), Rnd(3), Rnd(3)
    Debug.Print Rnd(3), Rnd(3), Rnd(3)
    Debug.Print "Results 3-B (With Randomized)"
    Rnd (-3)
    Randomize 3
    Debug.Print Rnd(3), Rnd(3), Rnd(3)
    Rnd (-3)
    Randomize 3
    Debug.Print Rnd(3), Rnd(3), Rnd(3): Debug.Print ""
    Debug.Print "Results 4-A (Not Randomized)"
    Debug.Print Rnd(3), Rnd(3), Rnd(3)
    Debug.Print Rnd(3), Rnd(3), Rnd(3)
    Debug.Print "Results 4-B (With Randomized)"
    Rnd (-3)
    Randomize 3
    Debug.Print Rnd(3), Rnd(3), Rnd(3)
    Rnd (-3)
    Randomize 3
    Debug.Print Rnd(3), Rnd(3), Rnd(3): Debug.Print ""
    End Sub
    ok so first run code and maximize your debug window lets have a look together

    you will see result 1-A, 1-B, 2-A, 2-B, 3-A, 3-B, 4-A, 4-B


    1-A and 2-A: these results are coded the same nothing changed ... the results do not match
    Code:
    Debug.Print Rnd(3), Rnd(3), Rnd(3)
    1-B and 2-B: these results are coded the same nothing changed ... the results do not match
    Code:
    Rnd(3)
    Randomize 3
    Debug.Print Rnd(3), Rnd(3), Rnd(3)
    3-A and 4-A: these results are coded the same nothing changed ... the results do not match
    Code:
    Debug.Print Rnd(3), Rnd(3), Rnd(3)
    3-B and 4-B: these results are coded the same nothing changed ... here the results are the same because we used Rnd(-3) and Randomize 3
    Code:
    Rnd(-3)
    Randomize 3
    Debug.Print Rnd(3), Rnd(3), Rnd(3)

    Edit: Rnd(#) in red
    Last edited by Max187Boucher; Sep 15th, 2012 at 08:16 PM.

  5. #5
    PowerPoster
    Join Date
    Aug 2011
    Location
    B.C., Canada
    Posts
    2,887

    Re: How exactly do you reinitialize RND in VB6?

    a simplier way to say this is call
    Code:
    Rnd (-1)
    Randomize 1
    Debug.Print Rnd()
    
    Rnd (-1)
    Randomize 1
    Debug.Print Rnd()
    and your results will always be the same


    but if you use

    Code:
    Rnd (1)
    Randomize 1
    Debug.Print Rnd()
    
    Rnd (1)
    Randomize 1
    Debug.Print Rnd()
    OR
    Code:
    Debug.Print Rnd()
    Debug.Print Rnd()
    this does not result in the same number... of course if you run code again it will start with the same number!

  6. #6
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    12,424

    Re: How exactly do you reinitialize RND in VB6?

    I'm wondering why there is a need to reset the random anyway. The whole purpose of using a random number is that it should be random. If you want the numbers to follow a pattern then maybe rnd is not the right choice.

  7. #7
    New Member
    Join Date
    Jan 2018
    Posts
    10

    Re: How exactly do you reinitialize RND in VB6?

    Quote Originally Posted by DataMiser View Post
    I'm wondering why there is a need to reset the random anyway. The whole purpose of using a random number is that it should be random. If you want the numbers to follow a pattern then maybe rnd is not the right choice.

    There is when it comes to it. not improperly implemented in other languages.

  8. #8
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    4,582

    Re: How exactly do you reinitialize RND in VB6?

    Quote Originally Posted by DataMiser View Post
    I'm wondering why there is a need to reset the random anyway. The whole purpose of using a random number is that it should be random. If you want the numbers to follow a pattern then maybe rnd is not the right choice.
    The main reason I've always heard for resetting is for repeatability when testing. You may want to run a series of "random" numbers through some processing and get a particular result. You may then want to save this result as the expected output from processing that series of numbers through the algorithm. Later, when you change code, you can reset the random function so that it will generate the same series of numbers so you can regression test your code to verify it comes up with the same expected output.

    If you want to run a known series of several million values through your code for testing without using a straight loop of sequential values, or saving a series of the millions of inputs you want to use, or writing your own algorithm to generate a pseudo random series of known values, the rnd function provides that capability.

  9. #9
    New Member
    Join Date
    Jun 2018
    Posts
    7

    Re: How exactly do you reinitialize RND in VB6?

    Because it's a pseudo random number generator and does not provide a truly random sequence.

    Rich

  10. #10
    PowerPoster
    Join Date
    Feb 2006
    Posts
    19,070

    Re: How exactly do you reinitialize RND in VB6?

    Seems to be working just as documented for me:

    Code:
    Private Sub Form_Load()
        AutoRedraw = True
        Rnd -1
        Randomize 1
        Print Rnd()
        Print Rnd()
        
        Print
    
        Rnd -1
        Randomize 1
        Print Rnd()
        Print Rnd()
    End Sub

  11. #11
    New Member
    Join Date
    Jan 2018
    Posts
    10

    Thumbs up Re: How exactly do you reinitialize RND in VB6?

    This is exactly what I use.

  12. #12
    Fanatic Member
    Join Date
    Dec 2012
    Posts
    595

    Re: How exactly do you reinitialize RND in VB6?

    Quote Originally Posted by DataMiser View Post
    I'm wondering why there is a need to reset the random anyway. The whole purpose of using a random number is that it should be random. If you want the numbers to follow a pattern then maybe rnd is not the right choice.
    There are some very good reasons for using a Pseudo Random generator (see the link below).

    http://www.vbforums.com/showthread.p...6Yates-shuffle

    This program was generated from a C++ program, which had the same Pseudo Random generator. To get a true random number, I use bCrypt.

    Code:
    Option Explicit
    
    Private Declare Function BCryptGenRandom Lib "bcrypt.dll" (ByVal hAlgorithm As Long, ByVal pbBuffer As Long, ByVal cbBuffer As Long, ByVal dwFlags As Long) As Long
    Private Declare Function BCryptOpenAlgorithmProvider Lib "bcrypt.dll" (ByRef hAlgorithm As Long, ByVal pszAlgId As Long, ByVal pszImplementation As Long, ByVal dwFlags As Long) As Long
    Private Declare Function BCryptCloseAlgorithmProvider Lib "bcrypt.dll" (ByVal hAlgorithm As Long, ByVal dwFlags As Long) As Long
    
    Private Function GenRandom(ByVal cbBuffer As Long) As Byte()
        Dim hRandomAlg As Long
        Dim pbBuffer() As Byte
        Dim lRet As Long
        lRet = BCryptOpenAlgorithmProvider(hRandomAlg, StrPtr("RNG"), 0&, 0)
        If lRet <> 0 Then
            GoTo ReleaseHandles
        End If
        ReDim pbBuffer(cbBuffer - 1)
        lRet = BCryptGenRandom(hRandomAlg, VarPtr(pbBuffer(0)), cbBuffer, 0)
        If lRet <> 0 Then
            GoTo ReleaseHandles
        End If
        GenRandom = pbBuffer
    ReleaseHandles:
        BCryptCloseAlgorithmProvider hRandomAlg, 0
    End Function
    J.A. Coutts

Posting Permissions

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



Featured


Click Here to Expand Forum to Full Width