CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Jan 2010
    Posts
    2

    Question How does VB Randomize REALLY work?

    I need to emulate VB's Rnd and Randomize functions, for use in other languages. The Rnd function is not a problem and I have code which works fine in several languages for the default true start seed (327680) - a quick test of the first million or so decimal random numbers from this seed agrees exactly with the VB's own Rnd output.

    The problem is that I need to be able to specify the start seed and Randomize hashes the seed it is given into a "true" seed. I've searched the internet for hours but cannot find any documentation that specifies how it does it - except for one article
    ( http://www.15seconds.com/issue/051110.htm )
    which is incorrect! This article states that:
    -----
    The Single (datatype) seed is 'moshed' into 2-byte seed (TwoByteSeed) value with the following XOR operations:
    TwoByteSeed = [byte1 XOR byte3] [byte2 XOR byte4];
    One additional XOR operation is performed on these two bytes, depending on the sign of the originally supplied seed as the following code snippet describes:
    If originalseed < 0 Then
    TwoByteSeed = TwoByteSeed XOR &h06C0
    Else
    TwoByteSeed = TwoByteSeed XOR &h0240
    End If
    -----

    This is demonstrably incorrect because offering a seed with a specific value in byte2 (with the other bytes all zero) should give exactly the same true seed as offering a seed with the same specific value in byte 4 (again with the others all zero). It doesn't, although the binary values do have marked similarities, and the additional XOR doesn't work either. However, I do think that the article may be partially correct.

    I have a small program which will search and discover what the true seed is for any Randomize seed (the true seed being the one which produces the correct VB Rnd output) but despite having generated hundreds of examples I still can't work out how Randomize manipulates the bits of the input seed to get the true seed. I do know that it adds a third fixed byte onto the end of the twobyte "hashed" seed (the byte value is 10000110b).

    My analytical skills have let me down here, and not being a coding expert I have no idea how to find or disassemble the VB Randomize source.

    Can anyone help, please?

  2. #2
    Join Date
    Jan 2006
    Location
    Fox Lake, IL
    Posts
    15,007

    Re: How does VB Randomize REALLY work?

    That would be 'hacking' I'd guess. Not allowed over at this forum. Why do you ask? This is a programming forum.

    Also, that article talks about VB.Net and this is the VB6 forum.

    btw -

    Welcome to the forums!
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  3. #3
    Join Date
    Jul 2006
    Location
    Germany
    Posts
    3,725

    Re: How does VB Randomize REALLY work?

    I always thought Randomize takes the value of the system time to produce a unique sequence of random numbers...

  4. #4
    Join Date
    Jan 2010
    Posts
    2

    Re: How does VB Randomize REALLY work?

    Quote Originally Posted by dglienna View Post
    That would be 'hacking' I'd guess. Not allowed over at this forum. Why do you ask? This is a programming forum.

    Also, that article talks about VB.Net and this is the VB6 forum.
    Well, I'm not a hacker! As I said, I'm writing a program which I want to be portable, and I don't want to use the VB built-in Randomize function, just emulate it. There doesn't seem to be any secret about how Rnd works (MS publish it), just the Randomize bit. The article I mentioned covers Visual Basic in general, and the same function is almost certainly used in both VB6 and VB.Net.

    Quote Originally Posted by WoF View Post
    I always thought Randomize takes the value of the system time to produce a unique sequence of random numbers...
    If you don't use Randomize the seed default is 327680;
    if you just use Randomize on its own, WITH OR WITHOUT a parameter, the session time is used as a seed (for Vista and Win7 it is the time elapsed since your session started, not the system clock, I believe - but I don't know about XP);
    if you perform Rnd(-1) then Randomize(n) the seed that is used is 'n' but it is hashed first, and it is the hashing subroutine that I am trying to work out.

    Sorry if I've stepped on a toe or two here - I'm just a hobbyist programmer (I do admit that I'm an OAP) trying to solve a problem.

  5. #5
    Join Date
    Jan 2006
    Location
    Fox Lake, IL
    Posts
    15,007

    Re: How does VB Randomize REALLY work?

    Well, VB6 and VB.Net are bound to be different. Once you 'crack' it correctly for 32-bit VB6, there will be 64-bit in VS2010 with the Net 4.0 Framework. I'd concentrate on that.
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

Tags for this Thread

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

Featured