• lp344 (unregistered) in reply to Cthulhon

    VB.NET != VBScript!

  • (cs) in reply to dubwai

    dubwai:
    Unfortunately (for me) I'm not kidding around.  I have mathlexia.

    I think that would be dismathia...

  • Nevermind (unregistered) in reply to ed
    Anonymous:
    Alex Papadimoulis:
        Length = CInt(Rnd * 1000) Mod 16


    What exactly does the line above? I'm not familiar with VB at all but common sense points that it gets a random number, multiplies it with 1000 and than computes the modulus with 16. If this is correct than Length will always be either 0 or 8, right?

    No, Rnd produces a number between 0 and 1.

  • Nevermind (unregistered) in reply to Nevermind

    Oops, missed a page

  • ed (unregistered) in reply to Nevermind
    Anonymous:

    No, Rnd produces a number between 0 and 1.


    I knew I'm missing something ;). I'm coming from the C world where rand() returns integer and I didn't even consider the possibility that Rnd in Visual Basic returns 'real' number. My bad.
  • Amit Patankar (unregistered) in reply to Grant Anderson

    You got that right ROFL

  • (cs) in reply to lucio
    lucio:

    dubwai:
    Unfortunately (for me) I'm not kidding around.  I have mathlexia.

    I think that would be dismathia...



    Actually, I think it'd be dysarithmia.

        dZ.
  • Andy (unregistered) in reply to DZ-Jay

    Never posted here before, but this one got me - the not-as-obvious bad part about this is that it doesn't matter how long the length is - only the first 4 characters really matter. Since it is all just random numbers generated by the random number generator (which is seeded by a 32 bit number), this only returns the first few characters of any one in 2^32 possible strings. I don't like the odds of missing a collision when there are only 4 billion available guids.

  • (cs)

    I just love to code VB. So here is som GUID code:
    Public Type GUID
        Data1 As Long
        Data2 As Integer
        Data3 As Integer
        Data4 As Integer
        Data5(5) As Byte
    End Type

    Private Type GUIDByteData
        data(15) As Byte
    End Type

    Public Function GenerateGuid() As GUID
    Dim i As Long
    Dim data As GUIDByteData
        Randomize     For i = 0 To 15
            data.data(i) = Int(Rnd * 256)
        Next

        LSet GenerateGuid = data
    End Function

    Public Function FormatGuid(GUID As GUID)
    Dim i As Long
    Dim Data1 As String
    Dim Data2 As String
    Dim Data3 As String
    Dim Data4 As String
    Dim Data5 As String

        Data1 = Right$("0000000" & Hex$(GUID.Data1), 8)
        Data2 = Right$("000" & Hex$(GUID.Data2), 4)
        Data3 = Right$("000" & Hex$(GUID.Data3), 4)
        Data4 = Right$("000" & Hex$(GUID.Data4), 4)
        For i = 0 To 5
            Data5 = Data5 & Right("0" & Hex(GUID.Data5(i)), 2)
        Next
       
        FormatGuid = "{" & Data1 & "-" & Data2 & "-" & Data3 & "-" & Data4 & "-" & Data5 & "}"
    End Function

  • (cs) in reply to Generic
    Generic:
    I just love to code VB. So here is som GUID code:
    Public Type GUID
        Data1 As Long
        Data2 As Integer
        Data3 As Integer
        Data4 As Integer
        Data5(5) As Byte
    End Type

    Private Type GUIDByteData
        data(15) As Byte
    End Type

    Public Function GenerateGuid() As GUID
    Dim i As Long
    Dim data As GUIDByteData
        Randomize

        For i = 0 To 15
            data.data(i) = Int(Rnd * 256)
        Next

        LSet GenerateGuid = data
    End Function

    Public Function FormatGuid(GUID As GUID)
    Dim i As Long
    Dim Data1 As String
    Dim Data2 As String
    Dim Data3 As String
    Dim Data4 As String
    Dim Data5 As String

        Data1 = Right$("0000000" & Hex$(GUID.Data1), 8)
        Data2 = Right$("000" & Hex$(GUID.Data2), 4)
        Data3 = Right$("000" & Hex$(GUID.Data3), 4)
        Data4 = Right$("000" & Hex$(GUID.Data4), 4)
        For i = 0 To 5
            Data5 = Data5 & Right("0" & Hex(GUID.Data5(i)), 2)
        Next
       
        FormatGuid = "{" & Data1 & "-" & Data2 & "-" & Data3 & "-" & Data4 & "-" & Data5 & "}"
    End Function

    For how wants to do it the corret way: Draft UUID specification (includes sample code) or use built in functions and APIs?

  • tekra (unregistered) in reply to Grant Anderson

    Z could be for Zed ...

  • Dave (unregistered) in reply to DZ-Jay
    DZ-Jay:
    Anonymous:
    Anonymous:

    Breaking news ...  we have just learned that the hexadecimal number system has lost it's '0's due to an hostile takeover bid.  A foreign invasion of  'Z's  - referring to the programmers having  'Zero' brains has however  agreed to  temporary fill the terrible gap left by the unexplained demise of the number '0'.  Stay tuned as we will keep you up to date should any other numbers in this numbering system follow '0''s lead and leave the number system ...

     

    Thats gonna kill the old "10 people in the world understand binary..." joke.

    1ZZZ1111Z1Z1Z1



    Not really.  It'll just be "1Z people in the world...", pronounced "Zuh-teen".

        dZ.



    LOL! I love "zuh-teen". That will be my next child's name.
  • (cs) in reply to Generic
    Generic:
    I just love to code VB. So here is som GUID code:
    Public Type GUID
        Data1 As Long
        Data2 As Integer
        Data3 As Integer
        Data4 As Integer
        Data5(5) As Byte
    End Type

    Private Type GUIDByteData
        data(15) As Byte
    End Type

    Public Function GenerateGuid() As GUID
    Dim i As Long
    Dim data As GUIDByteData
        Randomize

        For i = 0 To 15
            data.data(i) = Int(Rnd * 256)
        Next

        LSet GenerateGuid = data
    End Function

    Public Function FormatGuid(GUID As GUID)
    Dim i As Long
    Dim Data1 As String
    Dim Data2 As String
    Dim Data3 As String
    Dim Data4 As String
    Dim Data5 As String

        Data1 = Right$("0000000" & Hex$(GUID.Data1), 8)
        Data2 = Right$("000" & Hex$(GUID.Data2), 4)
        Data3 = Right$("000" & Hex$(GUID.Data3), 4)
        Data4 = Right$("000" & Hex$(GUID.Data4), 4)
        For i = 0 To 5
            Data5 = Data5 & Right("0" & Hex(GUID.Data5(i)), 2)
        Next
       
        FormatGuid = "{" & Data1 & "-" & Data2 & "-" & Data3 & "-" & Data4 & "-" & Data5 & "}"
    End Function


    But what if you were using VBScript and not VB as the original poster said?

        -dZ.
  • D (unregistered) in reply to DZ-Jay

    um, anyone else notice no breaks in the case statement?

    seems like it'll return all Z's

  • (cs) in reply to D
    Anonymous:
    um, anyone else notice no breaks in the case statement?

    seems like it'll return all Z's


    There's no breaks in vbscript select case
  • (cs)

    There are times when I get tired of hearing about how much VB sucks. I realize it's not the best language according to hardcore programmers, but I think that's mostly because it's A) easy as hell to learn, which in turn means that B) lots of idiots produce shitty code with it. Don't blame the language when someone screws it up.

    And to those that complain about how you set the return value to the function name (GenerateHex = GenerateHex & hex), just shut up. It's called syntax. In C you would say "return hex;", this is just a different way of doing that. You know why it's different? Because it's a different language. That's like complaining that you don't like using "Dim" to declare variables.

  • (cs) in reply to Generic

    Gee, Generic, very nice descriptive variable names you've got there. I especially like the clever way you (or at least the original coder) Dim'ed five string variables named 'Datan' in the last function instead of doing something silly like using an array - especially since the GUID and GUIDByteData types already used arrays earlier.

  • sir_flexalot (unregistered)

    Randomize timer... so there's a chance that once per day, you're getting dupes.  Plus, why rnd1000 mod 16?  Why not just rnd17?  I guess this guy tried rnd*16 and couldn't figure out why it only went 0-15. 

  • (cs) in reply to D
    Anonymous:
    um, anyone else notice no breaks in the case statement?


    # vb.h
    #define Case break; case
  • Suomynona (unregistered)

    I love the way it generates the same "random" GUID when two objects are created the same second. Even if the two same seconds are actually days or months apart. Maybe the GUIDs should be renamed to GNSUIDs though (global not so unique id).

  • (cs) in reply to Suomynona

    The ID could be used to identify what second of the day a code was generated.

  • Billy (unregistered) in reply to ed

    Rnd in VB returns a float between 0 and 1.  So you're basically getting a random number from 0 to 999, and then modulos 16 of that.


  • Suomynona (unregistered) in reply to Billy
    Anonymous:
    Rnd in VB returns a float between 0 and 1.  So you're basically getting a random number from 0 to 999, and then modulos 16 of that.


    No, it does not return a random number, it returns a pseudo-random number, and the sequence of pseudo-random numbers is determined by the seed that was fed into the PRNG (pseudo-random number generator) using Randomize.

    IMO it's worth a WTF that Microsoft doesn't bother to differentiate between randomness and pseudo-randomness in its very own documentation. I mean how's a clueless VBScripter supposed to learn if (s)he learns from clueless docs?

  • StringCheesian (unregistered) in reply to ed
    Anonymous:
    Alex Papadimoulis:
        Length = CInt(Rnd * 1000) Mod 16


    What exactly does the line above? I'm not familiar with VB at all but common sense points that it gets a random number, multiplies it with 1000 and than computes the modulus with 16. If this is correct than Length will always be either 0 or 8, right?

    It takes a random floating point number, from 0 to almost 1, and multiplies by 1000. At this point it's an integer between 0 and 999. Then it mods that by 16.
  • StringCheesian (unregistered) in reply to StringCheesian

    Ooops, disregard my last post please. I didn't see that there was a second page of posts... :$

  • (cs)

    This code is stupid and ugly, but really only has two major problems that are strongly related to one another.  Basically, this code is awful for generating GUIDs.

    First, I don't know VB at all, but I'm guessing that Rnd is not the best random number generator.  It's certainly not something I would trust for generating a GUID, especially since the method in which its used will only use the lower bits of the generated numbers, which tend to be the lowest quality bits in most non-cryptographic deterministic random number gneerators.

    That brings me to the second problem...

    CInt(Rnd * 1000) Mod 16

    This introduces a bias.  First, I'm guessing that Rnd is never supposed to produce a 1, just a number arbitrarily close to it.  Hopefully CInt does a floor operation, always rounding down.  This means that each integer from 0 to 999 is (supposedly, since most deterministic random number generators are pretty awful) equally likely.

    So, we've got:

    0-15 -> 0-15
    16-31 -> 0-15
    ...
    992-999 -> 0-7

    Oops, the numbers 0-7 are slightly more likely to show up than any other numbers.  I would consider this bias to be unacceptable in a GUID generator.  Though, in all likelihood it's probably swamped by the biases produced by the (very likely) poor quality of the deterministic random number generator sitting behind Rnd.

    Yes, the code produces a 'Z' instead of a 0.  This is kinda stupid and tells me that someone didn't really understand what was going on.  But it's probably not the biggest flaw for the use the code is being put to.

  • (cs) in reply to Andy
    Anonymous:
    Never posted here before, but this one got me - the not-as-obvious bad part about this is that it doesn't matter how long the length is - only the first 4 characters really matter. Since it is all just random numbers generated by the random number generator (which is seeded by a 32 bit number), this only returns the first few characters of any one in 2^32 possible strings. I don't like the odds of missing a collision when there are only 4 billion available guids.


    Oops, I missed that.  It was bothering me that they were seeding the generator with the time, but now that you mention it...  *sigh*  That's even worse than the flaws I pointed out.

  • (cs) in reply to Omnifarious

    It was bothering me that they were seeding the generator with the time...

    Well, maybe it should be seeded using the built-in guid funct.... oh.

    Seriously, the real WTFs here have already been pointed out, and are nothing to do with the obvious fucked-up-ness of the code but everything to do with the subtle unrandomness of the algorithm. And what's most scary is that this sort of subtle fuckup exists all over the place, and not always in code that makes you think "what the...." and delve deeper.

    Simon

  • WTFer (unregistered) in reply to JohnO

    [um][:)][:D][:O][:P][;)][:(][:S][:|][:'(][:$][H][:@][A][6][8-|][|-)][<:o)][:^)][:-][:#][Y][B][{][8][][au][pi][sn][O][co][st][mo][8o|][^o)][N][C][8-)][*-)][+o(][D][G][S][E][W][Z][}][~][ap][ip][I][&][^][U][N][C]

    JohnO:

    The real question for today is, what almost universally-recognized bad programming practice will someone defend today?  Will it be...

    Not using option explicit to force variable typing and declaration, or,

    Reusing a function parameter that is passed by reference as a local (I can't wait to hear the explanations for why this is smart in real-time programming situations).

    I just realized that I missed the Rnd * 1000 instead of rnd * 16 WTF.

  • Pope Innocent (unregistered)

    I could be wrong on this, but it seems that the TIMER() function returns a value from a millisecond-resolution timer.  If you call GenerateHex twice in the same millisecond, won't you get the same pseudorandom number from the call to RND?

  • (cs)

    Just trying to see what will happen if you call this routine 10 times

    Public Sub TestIt()
        Dim i As Integer
        For i = 1 To 10
            Debug.Print i, GenerateHex(i)
        Next
    End Sub

    What you would expect was

     1            3
     2            7F
     3            F8C
     4            A835
     5            3ZCZE
     6            6E9Z43
     7            1E4DA9Z
     8            6D951F59
     9            BC54EA7A3
     10           Z999DC1774

    (or something)

    Result

     7            7
     7            D4CBZ9B7
     14           81DF9E6E

    Problem the length parameter is passed as a reference and not as a value.

    So another bug.

     

  • (cs) in reply to DZ-Jay

    The mathematic equivalent of dyslexia is technically called dyscalculia.
    And it bugs the heck out of me when people use the names of real, diagnosable, chronic learning disabilities to describe their momentary brain burps.

  • (cs) in reply to DZ-Jay
    DZ-Jay:
    lucio:

    dubwai:
    Unfortunately (for me) I'm not kidding around.  I have mathlexia.

    I think that would be dismathia...



    Actually, I think it'd be dysarithmia.

        dZ.

    The mathematic equivalent of dyslexia is technically called dyscalculia. And it bugs the heck out of me when people use the names of real, diagnosable, chronic learning disabilities to describe their momentary brain burps.


  • (cs) in reply to Pope Innocent
    Anonymous:
    I could be wrong on this, but it seems that the TIMER() function returns a value from a millisecond-resolution timer.  If you call GenerateHex twice in the same millisecond, won't you get the same pseudorandom number from the call to RND?


    The TIMER() function is not being used in the code.

    RANDOMIZE TIMER means to seed (set starting value) the RND() function with the time.  The seeding is done once.  RND() calls after the first one have a different value to start working with.

    Sincerely,

    Gene Wirchenko

  • (cs) in reply to Suomynona

    Mr. Obvious:
    Anonymous:
    Rnd in VB returns a float between 0 and 1.  So you're basically getting a random number from 0 to 999, and then modulos 16 of that.


    No, it does not return a random number, it returns a pseudo-random number, and the sequence of pseudo-random numbers is determined by the seed that was fed into the PRNG (pseudo-random number generator) using Randomize.

    Why do people post stuff like this as if it isn't completely obvious?

    Everyone here already knows that.  There's need to specify that it's only a psuedo-random number and not a true random number.

  • (cs) in reply to DysgraphicProgrammer
    DysgraphicProgrammer:
    DZ-Jay:
    lucio:

    dubwai:
    Unfortunately (for me) I'm not kidding around.  I have mathlexia.

    I think that would be dismathia...



    Actually, I think it'd be dysarithmia.

        dZ.

    The mathematic equivalent of dyslexia is technically called dyscalculia. And it bugs the heck out of me when people use the names of real, diagnosable, chronic learning disabilities to describe their momentary brain burps.

    Who said it wasn't real in my case?  It bugs me when people make stupid-ass assumptions like this.

  • generalpf (unregistered) in reply to Gary and the Samoyeds

    You can't use IIf() in VBScript.  It's not there, just like Format(), which never fails to piss me off.

  • (cs) in reply to dubwai
    dubwai:
    DysgraphicProgrammer:
    DZ-Jay:
    lucio:

    dubwai:
    Unfortunately (for me) I'm not kidding around.  I have mathlexia.

    I think that would be dismathia...



    Actually, I think it'd be dysarithmia.

        dZ.

    The mathematic equivalent of dyslexia is technically called dyscalculia. And it bugs the heck out of me when people use the names of real, diagnosable, chronic learning disabilities to describe their momentary brain burps.

    Who said it wasn't real in my case?  It bugs me when people make stupid-ass assumptions like this.


    If you do, than I apologize for my unwarranted assumption. However, you did call it ‘mathlexia’ which is not the correct name.

    The people who say “I sometimes spell things wrong, so I must have dyslexia” or “I must be dyslexic today, because I got something backwards” water down the word and make it difficult to explain to others what is wrong with my brain.

    <o:p></o:p>Just as dyslexia is much more deep and complex than spelling errors, dyscalculia is more then being poor at arithmetic. Dyscalculia means that a brain is fundamentally formatted in a way that impedes mathematic learning. A programmer with dyscalculia is as unusual as a writer with dyslexia.  If you do legitimately have Dyscalculia, than I stand in awe of your achievement.

    I will now get off my soapbox. Sorry for the rant but this issue bushed one of my buttons.


  • (cs) in reply to DysgraphicProgrammer

    DysgraphicProgrammer:
    dubwai:

    Who said it wasn't real in my case?  It bugs me when people make stupid-ass assumptions like this.


    If you do, than I apologize for my unwarranted assumption. However, you did call it ‘mathlexia’ which is not the correct name.

    The people who say “I sometimes spell things wrong, so I must have dyslexia” or “I must be dyslexic today, because I got something backwards” water down the word and make it difficult to explain to others what is wrong with my brain.

    <?xml:namespace prefix = o /><o:p></o:p>Just as dyslexia is much more deep and complex than spelling errors, dyscalculia is more then being poor at arithmetic. Dyscalculia means that a brain is fundamentally formatted in a way that impedes mathematic learning. A programmer with dyscalculia is as unusual as a writer with dyslexia.  If you do legitimately have Dyscalculia, than I stand in awe of your achievement.

    I will now get off my soapbox. Sorry for the rant but this issue bushed one of my buttons.


    I've never been diagnosed but I've struggled with basic math since I was very young.  For example, in some sort of standardized testing I was given in 3rd grade, I scored in the 99.5 percentile for math concepts while at the same time scored in the 30th percentile for math computation.  My sister has similar struggles and was diagnosed with dyscalculia.  I forgot what the name was.  I don't think it was something well-known when I was growing up.  Anyway, it's kind of a sore spot because it really screwed me up in college because I was held back in math in grade school.  I wish I had been diagnosed because it would have helped me stay with my physics degree.  I know you didn't mean any harm, wuite the opposite, I'm sure.  My problem is not that I don't understand the math, it's that I can't keep the numbers straight in my head.  I still struggle to remember the multiplication tables.  Programming is actually a great fit because the computer excels at doing things that I find difficult.  So, I'm not sure I technically have dyscalculia but I am not being flip when I say that I am 'mathlexic.'

    P.S. Don't let beople bush your buttons.  It's better to be a button busher that to be the button bushee [;)]

  • charris (unregistered)

    <FONT style="BACKGROUND-COLOR: #ffffff" face=Verdana size=2>Sadly, it's fairly easy to create a GUID with BLOCKED SCRIPT</FONT>

    <FONT style="BACKGROUND-COLOR: #ffffff" face="Courier New" size=1>Set TypeLib = CreateObject("Scriptlet.TypeLib")</FONT><FONT style="BACKGROUND-COLOR: #ffffff" face="Courier New" size=1>
    </FONT><FONT face="Courier New"><FONT style="BACKGROUND-COLOR: #ffffff" size=1>NewGUID = TypeLib.Guid
    Set TypeLib = Nothing</FONT></FONT><FONT face="Times New Roman">

    </FONT>
  • Oisin G. (unregistered) in reply to ed

    RND will return a fraction between 0 and 1.

    it will then be multiplied by 1000, which has the effect of giving you a number between 0 and 1000, most likely a fraction. 

    CINT will remove the fractional part, giving you a integer between 0 and 1000.

    Mod 16 gives you the remainder of this integer after it has been divided by 16

    which leaves you with an integer between 0 and 15, inclusive.   

    btw, CInt(rnd * 16) has the same effect.

     

  • (cs) in reply to Oisin G.
    Oisin G.:

    RND will return a fraction between 0 and 1.

    it will then be multiplied by 1000, which has the effect of giving you a number between 0 and 1000, most likely a fraction. 

    CINT will remove the fractional part, giving you a integer between 0 and 1000.

    Mod 16 gives you the remainder of this integer after it has been divided by 16

    which leaves you with an integer between 0 and 15, inclusive.   

    btw, CInt(rnd * 16) has the same effect.

    Why did you feel the need to post such basic info?

  • Idiot (unregistered) in reply to Generic

    Why use a function at all?
    GUID = Hex(Int(Rnd * 65535)) & Hex(Int(Rnd * 65535)) & Hex(Int(Rnd * 65535)) & Hex(Int(Rnd * 65535))

  • Idiot (unregistered) in reply to Idiot

    Oops... that should obviously have been 65536

  • Idiot (unregistered) in reply to Idiot

    Finding the other error^H^H^H^H^Hfeature is left as an as an exercise for the reader ;)

  • Idiot (unregistered) in reply to Idiot

    Hint: here is a possible solution, albeit by sacrificing some of the randomness (there ought to be plenty enough left)

    Hex(Int(Rnd * 61440) + 4096) & Hex(Int(Rnd * 61440) + 4096) & Hex(Int(Rnd * 61440) + 4096) & Hex(Int(Rnd * 61440) + 4096)

  • stilgar (unregistered) in reply to Idiot

    Bit late maybe.. The 'Z' actually has a purpose other than the K00l. A leading Z will not get stripped away during conversions.

  • wtfer (unregistered) in reply to stilgar

    wtf

  • (cs) in reply to sir_flexalot
    Anonymous:
    Randomize timer... so there's a chance that once per day, you're getting dupes.  Plus, why rnd*1000 mod 16?  Why not just rnd*17?  I guess this guy tried rnd*16 and couldn't figure out why it only went 0-15. 


    umm... ok- did you just completely fail to realize that Hex is 0-15 inclusive. WTF?
    rnd*17 is NOT right... if he tried rnd*16 he wouldv at least been getting CLOSE!!
  • GeraldDew (unregistered)
    Comment held for moderation.

Leave a comment on “Putingz A Hex Onz Youz”

Log In or post as a guest

Replying to comment #40636:

« Return to Article