I don't use C# but when I put the original in the blender this is what came out
:
Code: Select all
Procedure.s GenerateDeviceToken()
Protected Dim rands.q(15)
Protected Dim hexValue.s(255)
Protected id.s = "", i, r.d, rand.d
;create 15 random numbers in range $00 to $FF
For i = 0 To 15
r = (Random(2147483647) * 2147483647 + Random(2147483647)) / 4611686016279904256 ;generate random double 0.0 to 1.0
rand = 4294967296.0 * r
rands(i) = ( Int(rand) >> ((3 & i) << 3) ) & 255
Next
;create hex string table for conversion
For i = 0 To 255
hexValue(i) = Right(Hex(i + 256), 2)
Next
;build the token string
For i = 0 To 15
id + hexValue(rands(i))
If (i = 3 Or i = 5 Or i = 7 Or i = 9)
id + "-"
EndIf
Next
ProcedureReturn id
EndProcedure
;test
Define x
For x = 1 To 1000
Debug GenerateDeviceToken() ;displays values such as E3CDBEFD-F9C2-68F5-0F12-8B01C2CA86F7"
Next
It can easily be shortened to this:
Code: Select all
Procedure.s GenerateDeviceToken()
Protected Dim rands.q(15)
Protected id.s = "", i, r.d, rand.d
;create 15 random numbers in range $00 to $FF
For i = 0 To 15
r = (Random(2147483647) * 2147483647 + Random(2147483647)) / 4611686016279904256 ;generate random double 0.0 to 1.0
rand = 4294967296.0 * r
rands(i) = ( Int(rand) >> ((3 & i) << 3) ) & 255
Next
;build the token string
For i = 0 To 15
id + Right(Hex(rands(i) + 256), 2)
If (i = 3 Or i = 5 Or i = 7 Or i = 9)
id + "-"
EndIf
Next
ProcedureReturn id
EndProcedure
;test
Define x
For x = 1 To 1000
Debug GenerateDeviceToken() ;displays values such as E3CDBEFD-F9C2-68F5-0F12-8B01C2CA86F7"
Next
@Edit: For completeness, the solutions have been reworked to function equally well on both 64-bit and 32-bit compilations to generate the desired level of randomness. Thank you Paul for alerting me to these issues dealing with Random() and the max values related to the Integer size in a compilation.