My application requires to generate GUID. I searched online documentation and forum for generating GUID but couldn’t find any API / method. It would be great if some one can point out how can i generate GUID using BrighScript .
There’s no built in utility to generate a GUID, but it wouldn’t be hard to write something that generates a pseudo-guid. Before you go that route, though, do you need a guid specifically, or just a randomly generated unique ID?
Here’s something I threw together really quick. It’s not guaranteed unique, but you’d have to generate an awful lot of them before you’d get dupes…
Function GenerateGuid() As String
' Ex. {5EF8541E-C9F7-CFCD-4BD4-036AF6C145DA}
Return "{" + GetRandomHexString(8) + "-" + GetRandomHexString(4) + "-" + GetRandomHexString(4) + "-" + GetRandomHexString(4) + "-" + GetRandomHexString(12) + "}"
End Function
Function GetRandomHexString(length As Integer) As String
hexChars = "0123456789ABCDEF"
hexString = ""
For i = 1 to length
hexString = hexString + hexChars.Mid(Rnd(16) - 1, 1)
Next
Return hexString
End Function
Thanks TheEndless for your response.
Yes i need GUID specifically not just a randomly generated unique ID. Main idea is to get unique ID each time app calls the function. Thanks for the sample code. It would be great if there is a guaranteed GUID generator.
This makes perfect sense. I think I can use RFC and combine it with couple of other params to generate GUID.
Still, i would say if BrightScript has build in support for GUID it would be easier for the developers
Out of curiosity, why are you needing to generate unique GUIDs so often from within the channel, and are you really going to be generating so many that my psuedo-guid function won’t work for you? With 32 randomly picked hex digits, I can’t imagine you’d ever be likely to hit a duplicate.
TheEndless,
Thanks for your psuedo-guid, really appreciate it. It can work for me but I was little concerned about “It’s not guaranteed unique”. I don’t want to introduce any bugs just because of the duplication. In my experience, this type of bugs are very difficult to track down because it appears only when there is a duplication and it may not appear in development environment at all.
In many applications, one does not have access to all previously generated GUIDs, but I can’t say if that’s true for the OP.
If Rnd returned truly random numbers, TheEndless’ algorithm would be pretty much as good as a real GUID. It’s possible, although extremely unlikely, for real GUIDs, produced according to the RFC, to produce duplicates too. In TheEndless’ algorithm, since Rnd is called in a predictable sequence, collisions are only as unlikely as two scripts starting with the same Rnd internal state, which actually isn’t all that unlikely. Rnd is not a cryptographically strong RNG and it’s probably not a good idea to use it as such.
“RokuMarkn” wrote:
In many applications, one does not have access to all previously generated GUIDs, but I can’t say if that’s true for the OP.
If Rnd returned truly random numbers, TheEndless’ algorithm would be pretty much as good as a real GUID. It’s possible, although extremely unlikely, for real GUIDs, produced according to the RFC, to produce duplicates too. In TheEndless’ algorithm, since Rnd is called in a predictable sequence, collisions are only as unlikely as two scripts starting with the same Rnd internal state, which actually isn’t all that unlikely. Rnd is not a cryptographically strong RNG and it’s probably not a good idea to use it as such.
–Mark
Mark, what is the seed for Rnd on the Roku? I assumed it would be the current tick (or something as equally high resolution), in which case, the likelihood of duplicate generation across a 32 character sequence seems extremely unlikely.
Your point about Rnd not being cryptographically strong is understood, but the OP just said he needed a unique ID in the form of a GUID. He never really expanded on why he needed them, hence my question about why he’d be generating them often enough that duplication was a concern.
It does use something like the current tick, which isn’t a very good seed. All it takes is for two boxes to boot at close to the same time and they could generate identical GUIDs. Granted, it does have to be VERY close to the same time, but depending on the cost of a collision, it may be a risk that the developer doesn’t want to take. Which gets back to your question to the OP about what the GUIDs will be used for.
I’m glad to report that I was mistaken about the seed for Rnd. It’s not based on the time, so a sequence of Rnds, while not of cryptographic quality, is not very likely to collide.
The function that TheEndless posted in this thread should work fine. In the extremely unlikely event that you need your GUID formatted as a version 4 GUID, you would force the first digit of the third group to be a “4” and the first digit of the fourth group to be 8,9,A or B.
“jaxim” wrote:
any update on this? I have a need to generate a GUID in a Roku app. Can someone share the GUID generator function?
Have a look at roDeviceInfo.getRandomUUID()