Roku Developer Program

Developers and content creators—a complete solution for growing an audience directly.
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
umesh
Level 7

Generating GUID using BrightScript

Hi,

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 .

Thanks and regards,
Tank
0 Kudos
15 REPLIES 15\
TheEndless
Level 9

Re: Generating GUID using BrightScript

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?
My Channels: http://roku.permanence.com - Twitter: @TheEndlessDev
Instant Watch Browser (NetflixIWB), Aquarium Screensaver (AQUARIUM), Clever Clocks Screensaver (CLEVERCLOCKS), iTunes Podcasts (ITPC), My Channels (MYCHANNELS)
0 Kudos
TheEndless
Level 9

Re: Generating GUID using BrightScript

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
My Channels: http://roku.permanence.com - Twitter: @TheEndlessDev
Instant Watch Browser (NetflixIWB), Aquarium Screensaver (AQUARIUM), Clever Clocks Screensaver (CLEVERCLOCKS), iTunes Podcasts (ITPC), My Channels (MYCHANNELS)
0 Kudos
umesh
Level 7

Re: Generating GUID using BrightScript

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.
0 Kudos
RokuMarkn
Level 8

Re: Generating GUID using BrightScript

You could implement a "name-based UUID" as per section 4.3 in RFC 4122. Use the device serial number from roDeviceInfo as the "node name".

--Mark
0 Kudos
umesh
Level 7

Re: Generating GUID using BrightScript

Thanks RokuMarkn,

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 Smiley Happy

Thanks again for your time and help
0 Kudos
TheEndless
Level 9

Re: Generating GUID using BrightScript

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.
My Channels: http://roku.permanence.com - Twitter: @TheEndlessDev
Instant Watch Browser (NetflixIWB), Aquarium Screensaver (AQUARIUM), Clever Clocks Screensaver (CLEVERCLOCKS), iTunes Podcasts (ITPC), My Channels (MYCHANNELS)
0 Kudos
umesh
Level 7

Re: Generating GUID using BrightScript

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.

Thanks again to all for the help Smiley Happy
0 Kudos
destruk
Level 10

Re: Generating GUID using BrightScript

You could check for duplication and have it regenerate itself.
0 Kudos
RokuMarkn
Level 8

Re: Generating GUID using BrightScript

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
0 Kudos