- Feature Articles
- CodeSOD
- Error'd
- Forums
-
Other Articles
- Random Article
- Other Series
- Alex's Soapbox
- Announcements
- Best of…
- Best of Email
- Best of the Sidebar
- Bring Your Own Code
- Coded Smorgasbord
- Mandatory Fun Day
- Off Topic
- Representative Line
- News Roundup
- Editor's Soapbox
- Software on the Rocks
- Souvenir Potpourri
- Sponsor Post
- Tales from the Interview
- The Daily WTF: Live
- Virtudyne
Admin
VB.NET != VBScript!
Admin
I think that would be dismathia...
Admin
No, Rnd produces a number between 0 and 1.
Admin
Oops, missed a page
Admin
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.
Admin
You got that right ROFL
Admin
Actually, I think it'd be dysarithmia.
dZ.
Admin
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.
Admin
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
Admin
For how wants to do it the corret way: Draft UUID specification (includes sample code) or use built in functions and APIs?
Admin
Z could be for Zed ...
Admin
LOL! I love "zuh-teen". That will be my next child's name.
Admin
But what if you were using VBScript and not VB as the original poster said?
-dZ.
Admin
um, anyone else notice no breaks in the case statement?
seems like it'll return all Z's
Admin
There's no breaks in vbscript select case
Admin
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.
Admin
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.
Admin
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.
Admin
# vb.h
#define Case break; case
Admin
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).
Admin
The ID could be used to identify what second of the day a code was generated.
Admin
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.
Admin
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?
Admin
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.
Admin
Ooops, disregard my last post please. I didn't see that there was a second page of posts... :$
Admin
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.
Admin
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.
Admin
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
Admin
[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]
Admin
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?
Admin
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.
Admin
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.
Admin
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.
Admin
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
Admin
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.
Admin
Who said it wasn't real in my case? It bugs me when people make stupid-ass assumptions like this.
Admin
You can't use IIf() in VBScript. It's not there, just like Format(), which never fails to piss me off.
Admin
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.
Admin
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 [;)]
Admin
<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><FONT face="Courier New"><FONT style="BACKGROUND-COLOR: #ffffff" size=1>NewGUID = TypeLib.Guid
Set TypeLib = Nothing</FONT></FONT><FONT face="Times New Roman">
Admin
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.
Admin
Why did you feel the need to post such basic info?
Admin
Why use a function at all?
GUID = Hex(Int(Rnd * 65535)) & Hex(Int(Rnd * 65535)) & Hex(Int(Rnd * 65535)) & Hex(Int(Rnd * 65535))
Admin
Oops... that should obviously have been 65536
Admin
Finding the other error^H^H^H^H^Hfeature is left as an as an exercise for the reader ;)
Admin
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)
Admin
Bit late maybe.. The 'Z' actually has a purpose other than the K00l. A leading Z will not get stripped away during conversions.
Admin
wtf
Admin
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!!