Creating a unique ID for a booking

Hey guys,

Could anyone enlighten me on how to create an alphanumeric unique Id. for a booking record ? I thought maybe could be somehow related to a time/date… it doesn’t have to be understandable and preferably within the 9-12 chars, but could be longer if it is needed…

I really don’t know how to do this… but worse, I don’t even know how to “think” at this… never done it before…

Ideas ?
TIA.
Roman

Check out what an UUID is. MBS has UUIDMBS. I’m sure that there are implementations in Xojo code. Perhaps in Kem Tekinay’s string module?

or just a hash of timestamp plus random number?

a hash of timestamp plus random number… plus a prefix… yeah… could be… how do I do it ? :slight_smile:

may be you could use the barcode that is on almost every book
or
search this forum and find this thread about UUID :
https://forum.xojo.com/18029-native-uuid-generation

or also this method :

[code]Function NewUUID() As String
'* Copyright © 2007, Taylor Design
'* All rights reserved.
'*
'* Redistribution and use in source and binary forms, with or without
'* modification, are permitted provided that the following conditions are met:
'* * Redistributions of source code must retain the above copyright
'* notice, this list of conditions and the following disclaimer.
'* * Redistributions in binary form must reproduce the above copyright
'* notice, this list of conditions and the following disclaimer in the
'* documentation and/or other materials provided with the distribution.
'* * Neither the name of the company nor the
'* names of its contributors may be used to endorse or promote products
'* derived from this software without specific prior written permission.
‘*
‘* THIS SOFTWARE IS PROVIDED BY Taylor Design ``AS IS’’ AND ANY
'* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
'* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
'* DISCLAIMED. IN NO EVENT SHALL Taylor Design BE LIABLE FOR ANY
'* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
'* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
'* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
'* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
'* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
'* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

'Creates a Universally Unique Identifier.
'This method creates its UUID using only RB framework
'methods. The resulting UUID is 36 bytes in length.

'For this method to fail there would have to be two machines with
'identical MAC addresses, clock times, boot times, and random number
'generators (motherboard device). The odds are extremely small that this
'could ever occur, and in fact should never occur with network machines.

'Variables
Dim Block As MemoryBlock
Block = New MemoryBlock(36)

'Uniqueness in space - machine MAC address, 12 bytes.
Dim nic As NetworkInterface
Dim mac As String

If System.NetworkInterfaceCount > 0 Then
nic = System.GetNetworkInterface(0)
mac = nic.MACAddress
mac = ReplaceAll(mac, “:”, “”)
Block.StringValue(0, 12) = mac
Else
'If there’s no network interface, a random number is better than nothing.
'Note that the last 4 bytes of the second Rnd will be overwritten.
Block.DoubleValue(0) = Rnd
Block.DoubleValue(8) = Rnd
End If

'Uniqueness in time - TotalSeconds + machine Microseconds.
'TotalSeconds, 8 bytes.
Dim DateTime As Date = New Date
Block.DoubleValue(12) = DateTime.TotalSeconds

'Microseconds, which is based on boot time, 8 bytes.
Block.DoubleValue(20) = Microseconds

'Further uniqueness in space/time - random number generated by
'motherboard (space) based on boot time and seed, 8 bytes.
Block.DoubleValue(28) = Rnd

'Get UUID.
Return Block.StringValue(0, 36)
End Function
[/code]

Hey Jean… that looks good, but NewUUID returns a sequence of characters that doesn’t look like a string. It needs to be something that can be typed…

Something like this?

[code]Function RandomStr(minLen As Integer, maxLen As Integer, lowerc As Boolean, upperc As Boolean, numeric As Boolean, symbols As Boolean) As String
// Select Characters to pick from
Dim sGrabBag As String
If lowerc Then sGrabBag = sGrabBag + “abcdefghijklmnopqrstuvwxyz”
If upperc Then sGrabBag = sGrabBag + “ABCDEFGHIJKLMNOPQRSTUVWXYZ”
If numeric Then sGrabBag = sGrabBag + “1234567890”
If symbols Then sGrabBag = sGrabBag + “!@#$%^&*()_-+”

// How many do we have?
Dim iBagChars, iRandChar As Integer
iBagChars = Len(sGrabBag)

// Init rnd
Dim oRand As New Random

// Pick Length
Dim iLength As Integer
oRand.RandomizeSeed
iLength = oRand.InRange(minLen, maxLen)

// Pick iLength characters
Dim sResult, sChar As String
For i As Integer = 1 To iLength
oRand.RandomizeSeed
iRandChar = oRand.InRange(1, iBagChars)
sChar = Mid(sGrabBag, iRandChar, 1)
sResult = sResult + sChar
Next i

Return sResult
End Function
[/code]
For 9-12 characters a-z, A-Z and 0-9 but no symbols do:
Dim s As String = RandomStr(9, 12, True, True, True, False)

Edit: Oh, if it is something that needs to be typed over, I suggest using a font like Courier or filter out things like 1, 0, l (lowercase L) I (uppercase i) etc. to avoid confusion.

1 Like

Lovely…

I think that’s going to work… how many chances are that it will return a repeated string ?

Most of the solution here generate quite long codes.
OP was asking for maybe 9…12

I wonder what the booking reference is for
If what is required is a unique id for each saved database row, then try this:

Generate a small string of 2 random letters eg YH
Append the date time formatted as YYMMDDHHmm

So a booking gets something like RF1605131423
The next one generated might be LP1605131424
I cant think of a way that that could get duplicated unless you create two bookings within a single minute, both of which get the same two random letters at the beginning.

just noticed that booking does not involve books … :wink: mistake in my previous post !

You are right Jeff, it’s an id for each saved database row…

Both your solution and Marco’s seem like a good approach… I still wonder how much “randomness” should be applied here…

Anyway… OT (not that I am trying to do something like that but…) How do airlines create their reservation ID’s ? they are generally 6 chars long and they DO create many of them per minute…

I also feel stupid to ask this… but… Can I not use a sequential numbering scheme ?

Of course you can.
Many database systems have an autoincrementing feature that does exactly this.
It does mean that when someone wants to search for the booking, there is a chance of transcription errors

(eg search for 62653675 but accidentally type 62656375)

Having letters in there (which vary) reduces the odds of typing the wrong code, but a valid one.
6 numerical digits allows for 1 million bookings (000000… 999999) before you run out of space.

[quote=265919:@Roman Varas]Lovely…

I think that’s going to work… how many chances are that it will return a repeated string ?[/quote]
I think with 6 chars [a-zA-Z0-9] there are 56~57 billion possible strings. Using 12 doesn’t fit my calculator. So yes it is possible that it returns a repeated string but unlikely.

In your last reply you say that it’s an id for each database row. I assumed there were generated independently/offline. In that case, I would just use the primary. Or generate some short 4 char and check if you have it already.

You’ve all been very helpful. thanks a lot!!!

If you might EVER need to synch this data with another computer/database, you need to insure you “key” would be unique across all data sources… so a UUID is best. sure it it is long, but it is unique… and the question is… does your app NEED to expose it to the end user? If not, does it matter how big it is?