PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197
VS 2010 [RESOLVED] randomize-VBForums
Results 1 to 9 of 9

Thread: [RESOLVED] randomize

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Jan 2013
    Posts
    398

    Resolved [RESOLVED] randomize

    When using randmize() I, believe the seed is provided by the system timer. I know you can set the seed yourself, I would prefer not to do this. For testing and debugging I want to know value of the seed provided by the system timer.
    Thanks
    George

  2. #2
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Pointless Forest 38.517,-92.023
    Posts
    9,045

    Re: randomize

    Use the alternative to Randomize and Rnd please. That is the Random class.

    If you are trying to create the same set of random numbers over and over for testing pass the same integer to the constructor of the Random class.
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  3. #3
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    31,877

    Re: randomize

    I believe it is the current time down to the milisecond. However, that said, I'm guessing you're using the randomize() function along with rnd() ... correct? if that's the case, don't... instead, use the more reliable Randomize class. Randomize() and Rnd() are hold overs from prior VB versions, which, while they work in a pinch, have some limitations. The Randomize class, while still not completely perfect (as nothing ever will be), at least attempts to move past those limitations.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  4. #4
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    100,339

    Re: randomize

    As suggested, the Random class should be used in preference to Randomize and Rnd. The Random constructor basically replaces Randomize and the Next function basically replaces Rnd. Just like Randomize, the Random constructor allows you to specify a seed value. If you want to know what the seed is, provide it yourself. That is useful for testing sometimes because it means that you can know exactly what sequence of pseudo-random numbers be generated. In situations like this, it can be useful to use conditional compilation, e.g.
    vb.net Code:
    1. #If DEBUG Then
    2.     Private rng As New Random(1234)
    3.     Private rng As New Random
    4. #End If
    In that case, the first line of code will be used in a Debug build and thus when you are testing, then the second line of code will be used in a Release build, which goes out to Production.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Pointless Forest 38.517,-92.023
    Posts
    9,045

    Re: randomize

    I'd only change the instances of the Random class to Shared. In this particular case it probably doesn't matter.

    Code:
    #If DEBUG Then
        Private Shared rng As New Random(1234)
    #Else
        Private Shared rng As New Random()
    #End If
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  6. #6
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    100,339

    Re: randomize

    Quote Originally Posted by dbasnett View Post
    I'd only change the instances of the Random class to Shared. In this particular case it probably doesn't matter.

    Code:
    #If DEBUG Then
        Private Shared rng As New Random(1234)
    #Else
        Private Shared rng As New Random()
    #End If
    You may actually specifically not want to do that in this case because you might want the same sequence of numbers to start each time you opened a form. If it was just the same sequence required per application session, rather than per form instance, then Shared might be the way to go.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  7. #7
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    32,609

    Re: randomize

    One thing I'd clarify: The .Next method of Random is NOT like Rnd(). It's VASTLY better. The .Next method is what people likely always wanted Rnd() to do. You set the range you want, and get a random integer in that range. No more calculations to turn the output of the method into a number in the range you want. Rnd() sucked. It's just all that was available for VB6. There were always questions about it as new people encountered it. Randomize pretty much sucked, too, because it was often unclear how you were supposed to use it. The fact that it had to be called just one time was barely documented, and the ramifications of calling it more than once were often left to the user to discover. Randomize and Rnd were lousy implementations of a relatively simple idea. Random is just flat out better.
    My usual boring signature: Nothing

  8. #8
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    100,339

    Re: randomize

    Quote Originally Posted by Shaggy Hiker View Post
    The .Next method of Random is NOT like Rnd().
    The Random.NextDouble method does exactly what Rnd does and the Random.Next method does the scaling and rounding internally on the result of NextDouble that the developer has to do for themselves with Rnd.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  9. #9
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    32,609

    Re: randomize

    Right. I felt you were underselling the advantages of .Next over Rnd(). Questions about how to use Rnd(), and the slightly arcane math around turning that into an integer in a range, were fairly common over in the Classic VB. It wasn't really necessary, either. Sure, without a class behind it, there would have had to be a third method (Randomize being the first) to get random integers, but MS didn't shy away from adding methods, so why not add the one that most people would use? Random doubles just aren't as common as random integers, yet they made that just tough enough to confuse people when a further method, along the lines of Next() wouldn't have hurt anything.
    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
  •  



Featured


Click Here to Expand Forum to Full Width