Real Software Forums

The forum for Real Studio and other Real Software products.
[ REAL Software Website | Board Index ]
It is currently Thu Jul 18, 2019 5:50 am
xojo

All times are UTC - 5 hours




Post new topic Reply to topic  [ 19 posts ]  Go to page 1, 2  Next
Author Message
 Post subject: Re: GUID Creation
PostPosted: Fri Jun 01, 2012 2:36 pm 
Offline
User avatar

Joined: Tue Sep 02, 2008 3:01 pm
Posts: 193
Location: Switzerland
For database aficionados:
Function GUID() As String
Dim db As New REALSQLDatabase

If db.Connect Then Return db.SQLSelect("select hex( randomblob(4)) " _
+ "|| '-' || hex( randomblob(2)) " _
+ "|| '-' || '4' || substr( hex( randomblob(2)), 2) " _
+ "|| '-' || substr('AB89', 1 + (abs(random()) % 4) , 1) " _
+ "|| substr(hex(randomblob(2)), 2) " _
+ "|| '-' || hex(randomblob(6)) AS GUID").Field("GUID")

Return "ERROR"
End Function


Added remarks:
1) randomblob is a SQLite core function, and in the SQLite docu it is explicitly mentioned to use for generating a guid. See: http://www.sqlite.org/lang_corefunc.html
2) The function above generates a type 4 GUID, according to the documentation available here: https://secure.wikimedia.org/wikipedia/en/wiki/Uuid
3) Version 4 UUID (random) are not 100% unique. However, if constructed according to the documentation, then a collision is highly unlikely. "... after generating 1 billion UUIDs every second for the next 100 years, the probability of creating just one duplicate would be about 50%. The probability of one duplicate would be about 50% if every person on earth owns 600 million UUIDs."
From: "Random UUID probability of duplicates", https://secure.wikimedia.org/wikipedia/en/wiki/Uuid#Random_UUID_probability_of_duplicates

_________________
"Stop deluding yourself with wishful thinking and find a solution that works for what you need to do"
Tim Hare - on forums.realsoftware.com


Last edited by oleman108 on Sat Jun 02, 2012 1:37 pm, edited 1 time in total.

Top
 Profile  
Reply with quote  
 Post subject: Re: GUID Creation
PostPosted: Fri Jun 01, 2012 3:39 pm 
Offline
User avatar

Joined: Sun Aug 12, 2007 10:10 am
Posts: 1086
Location: Boiling Springs, SC
Random String might be useful for custom generated 'serials' etc ;-)

Private Function RandStr(ranlen As Integer, nmb As Boolean, lcs As Boolean, ucs As Boolean, smb As Boolean) As String
Dim source As String = ""
if nmb = True then source = source + "1234567890"
if lcs = True then source = source + "abcdefghijklmnopqrstuvwxyz"
if ucs = True then source = source + "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
if smb = True then source = source + "|@#~$%()=^*+[]{}-_"
Dim chrstr,num As String
Dim NumChar,start,i As Integer
NumChar = source.Len( )
for i = 1 To ranlen
Dim r as New Random
start = r.InRange( 1, NumChar )
num = source.Mid( start, 1 )
chrstr = chrstr + num
Next
return chrstr
End Function

_________________
Matthew A. Combatti
Real Studio 2012 r1.2

Visit Xojo Developer's Spot!
Systems I Use:
Windows XP/Windows Vista/Windows Server 2008 r2/Windows 7/Windows 8
Mac OSX 10.5/Mac OSX 10.6/Mac OSX Server/Ubuntu/Debian/Suse/Red Hat/
Windows Server 2011/CentOS 5.4 /ReactOS/SimOS

~All Xojo Compatible~


Top
 Profile  
Reply with quote  
 Post subject: Re: GUID Creation
PostPosted: Fri Jun 01, 2012 4:02 pm 
Offline

Joined: Wed Mar 22, 2006 11:15 am
Posts: 712
Location: Southern California
For the record, I wouldn't trust anything that relies entirely on random functions to work under heavy use. Random functions on computers are not truly random, and can generate identical sequences much more easily then you might expect.

Though it might be rare, both the database and random string code could lead to a collision.

I know the OS functions do something similar to what I'm doing in UUID, which is basically mixing multiple information sources (MAC; datetime; microseconds since boot; and random). It's still not 100% guaranteed, but you should never see a collision with the OS APIs or UUID. I believe (correct me if wrong Christian) that MBS uses the underlying OS APIs.

_________________
Daniel L. Taylor
Custom Controls for Real Studio WE!
Visit: http://www.webcustomcontrols.com/


Top
 Profile  
Reply with quote  
 Post subject: Re: GUID Creation
PostPosted: Fri Jun 01, 2012 4:20 pm 
Offline
User avatar

Joined: Tue Sep 02, 2008 3:01 pm
Posts: 193
Location: Switzerland
taylor-design wrote:
For the record, I wouldn't trust anything that relies entirely on random functions to work under heavy use. Random functions on computers are not truly random, and can generate identical sequences much more easily then you might [...].

"after generating 1 billion UUIDs every second for the next 100 years, the probability of creating just one duplicate would be about 50%"
from: "Random UUID probability of duplicates", https://secure.wikimedia.org/wikipedia/en/wiki/Uuid

_________________
"Stop deluding yourself with wishful thinking and find a solution that works for what you need to do"
Tim Hare - on forums.realsoftware.com


Top
 Profile  
Reply with quote  
 Post subject: Re: GUID Creation
PostPosted: Fri Jun 01, 2012 4:36 pm 
Offline

Joined: Wed Mar 22, 2006 11:15 am
Posts: 712
Location: Southern California
oleman108 wrote:
taylor-design wrote:
For the record, I wouldn't trust anything that relies entirely on random functions to work under heavy use. Random functions on computers are not truly random, and can generate identical sequences much more easily then you might [...].

"the probability is about 0.00000000006 (6 × 10−11), equivalent to the odds of creating a few tens of trillions of UUIDs in a year and having one duplicate"
from: "Random UUID probability of duplicates", https://secure.wikimedia.org/wikipedia/en/wiki/Uuid


Looking more closely at your code, and assuming RandomBlob is working correctly, I believe that is true for the database code you provided.

But what really caught my eye was the string code. I would not rely on calling Random.InRange. And yes, I have seen code like that produce a collision in a surprisingly short amount of time.

_________________
Daniel L. Taylor
Custom Controls for Real Studio WE!
Visit: http://www.webcustomcontrols.com/


Top
 Profile  
Reply with quote  
 Post subject: Re: GUID Creation
PostPosted: Sat Jun 02, 2012 10:33 am 
Offline

Joined: Fri Jan 05, 2007 12:21 pm
Posts: 151
Location: Scotland
I'll also stick in a plug here for my UUID class, available from ARBP repository on a BSD style licence, which generates type 1,3 and 4 UUIDs. Type 4 is the random type; type 1 is time based.


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 19 posts ]  Go to page 1, 2  Next

All times are UTC - 5 hours


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group