Android Question [B4X] GUID uniqueness

udg

Expert
Licensed User
Hi all,
I read a few threads about the generation of a GUID (e.g. this one). My question is more about the real uniqueness a generated GUID is supposed to have.
I mean, if hundreds or thousands of users activate a given app at the same time would the inherent ability of the random generator still guarantee uniqueness? Is there any indication on a threshold where this is believed to fail?
Should I deploy a protocol where, upon an eventual duplicate is received by the server, a message is sent to the app causing a new GUID generation and so on until a unique one is received?
Should I evaluate the introduction of a "chaos generation" mechanism like randomly moving a finger on a pad as a way to strengthen the uniqueness?

I need real uniqueness (multiplatform) on a potentially large number of installations. GUID will be used to send strictly private FCM/MQTT messages.

TIA
 

DonManfred

Expert
Licensed User
My 2cent:

If you really need a UNIQUE GUID then, i fear, you need to track all of them in a Server.

I suggest (this is probably what i would do) to create a Serverapp which generates a GUID on behalf, write it to DB and return the unique GUID to your app to use it...
 
Upvote 0

BillMeyer

Well-Known Member
Licensed User
My 2cent:

If you really need a UNIQUE GUID then, i fear, you need to track all of them in a Server.

I suggest (this is probably what i would do) to create a Serverapp which generates a GUID on behalf, write it to DB and return the unique GUID to your app to use it...

I do it like this and it works very well !!
 
Upvote 0

LucaMs

Expert
Licensed User
I read a few threads about the generation of a GUID (e.g. this one).
That Erel's routine, if I'm not mistaken, creates 32 positions with values from 0 to 15, in practice. So it is equivalent to a power elevation with base 16 and elevation 32 (Power (16, 32)), obtaining such a huge value that it would not even be possible to memorize in a long, which even can exceed 9 million billion.

1584714220572.png


It is practically impossible to randomly create two identical ones.
 
Last edited:
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Use the code from the first post: https://www.b4x.com/android/forum/threads/b4x-guid.99529
You don't need to worry about uniqueness.

BTW, if you want to "test" it:
B4X:
Sub Process_Globals
    Dim set As B4XSet 'B4XCollections
End Sub

Sub AppStart (Args() As String)
    set.Initialize
    For i = 1 To 10000000
        set.Add(GUID)
        If i Mod 1000000 = 0 Then
            Log($"${i}: ${NumberFormat(set.Size, 0, 0)}"$)
        End If
    Next
End Sub

Sub GUID As String
    Dim sb As StringBuilder
    sb.Initialize
    For Each stp As Int In Array(8, 4, 4, 4, 12)
        If sb.Length > 0 Then sb.Append("-")
        For n = 1 To stp
            Dim c As Int = Rnd(0, 16)
            If c < 10 Then c = c + 48 Else c = c + 55
            sb.Append(Chr(c))
        Next
    Next
    Return sb.ToString
End Sub
 
Upvote 0

LucaMs

Expert
Licensed User
I am curious to know if there is a sw capable of transcribing this number into words (English, Italian or any other language).
1584875785810.png

I can't read it 😄
 
Upvote 0

udg

Expert
Licensed User
My concern was about the random generator. When calling the rnd function on any given device it's not surprise that a GUID won't repeat easily .
But if thousands of devices could potentially run the exact same code at the same time, what keeps their random generators to produce identical twins?
I could understand if the generator is initialized by some "secret" unique key/ID available only deep in Android, but if it is initialized by any eventual common code (e.g. datetime) the possibility that a collision happens becomes real. Don't you think so?

IMHO, having a server where unique guids are produced, stored and pushed to all the devices seems a must where you absolutely need that kind of uniqueness.
 
Upvote 0
Top