generate 6 digit unique id

M

Marc

Hi all,
I have to generate and send to a printer many 6 digit
alphanumeric strings. they have to be unique but I cannot check in a
database or something like that if it have already been printed. the
string has also to seem a random one and it cannot have an apparence
of a sequence.
My first approach is to do it with a decimal counter and find
and use an encryption alghorithm that converts each 6 digit decimal
number to a 6 digit alphanumeric string so then the string should have
to be unique.

Any idea on which algorithm or encryptor should I have to use?

Thanks in advance,

Marc Soleda
 
G

Guest

Does "cannot check in a database or something like that..." preclude checking
a dictionary for the 6 character key? If so, why put the constraint on
yourself. You haven't said what the maximum number of strings you will send
that need to be unique. As you approach 36^7 strings, your chance for
uniqueness diminishes, though that will be far off.
 
M

Marc

The thing is that this string must be sent to a printing device.
Imagine: an inkjet printer that prints each string on bottles. this
number will be used for a daily lottery, that's why it has to be daily
unique. Each day I will reset the counter and, at most, I'll have to
print 400000 units.
The reason why I don't want to check any database and that I prefer an
algorith is because I have to transmit between 6 ad 7 string per
minute sincronized with the bottles speed so I can't lose time
querying the database.

Marc
 
L

Lloyd Sheen

Marc said:
The thing is that this string must be sent to a printing device.
Imagine: an inkjet printer that prints each string on bottles. this
number will be used for a daily lottery, that's why it has to be daily
unique. Each day I will reset the counter and, at most, I'll have to
print 400000 units.
The reason why I don't want to check any database and that I prefer an
algorith is because I have to transmit between 6 ad 7 string per
minute sincronized with the bottles speed so I can't lose time
querying the database.

Marc

Sorry I have followed entire conversation but perhaps each day you could
create a new list of numbers offline to the process. They could be stored
in any number of places XML/Database etc and when you do you processing you
can then extract the numbers one at a time. Since you are doing the
sequence creation during "non-print" time you can make it as durable as
possible.

Hope this helps
Lloyd Sheen
 
M

Marc

Sorry I have followed entire conversation but perhaps each day you could
create a new list of numbers offline to the process. They could be stored
in any number of places XML/Database etc and when you do you processing you
can then extract the numbers one at a time. Since you are doing the
sequence creation during "non-print" time you can make it as durable as
possible.

Hope this helps
Lloyd Sheen- Ocultar texto de la cita -

- Mostrar texto de la cita -

There's no problem if the sequences are the same between days. The
important thing is that they are unique during each day. Even I had
enough time to query a database or xml schema I need to use sequences
of 6 alphanumeric digits that seem alphanumeric. I mean the sequences
cannot be 000001, 000002, .... 00000A, .... they shoul be 43TDZ,
P4AS33, ...
Any idea?

Thanks,
Marc
 
R

rowe_newsgroups

There's no problem if the sequences are the same between days. The
important thing is that they are unique during each day. Even I had
enough time to query a database or xml schema I need to use sequences
of 6 alphanumeric digits that seem alphanumeric. I mean the sequences
cannot be 000001, 000002, .... 00000A, .... they shoul be 43TDZ,
P4AS33, ...
Any idea?

Thanks,
Marc

How about:

Module Module1

Public Sub Main()
For i As Integer = 0 To 100000
Console.WriteLine(GetNextString())
Next
End Sub

Dim rnd As New Random()

Public usedStrings As New List(Of String)()

Public Function GetNextString() As String

Dim theString As String = GetRandomString()

While usedStrings.Contains(theString)
theString = GetRandomString()
End While

usedStrings.Add(theString)
Return theString

End Function

Public Function GetRandomString() As String
Dim rndString As String = ""

For i As Integer = 0 To 5
If rnd.Next(0, 2) = 0 Then
rndString += Microsoft.VisualBasic.Chr(rnd.Next(65,
91))
Else
rndString += Microsoft.VisualBasic.Chr(rnd.Next(48,
58))
End If
Next

Return rndString
End Function

End Module

It gets slower as it runs (as it has more strings to search in the
list), but at 6 or 7 strings a minute it would take weeks before the
pause would cause a problem.

Thanks,

Seth Rowe
 
M

Marc

How about:

Module Module1

Public Sub Main()
For i As Integer = 0 To 100000
Console.WriteLine(GetNextString())
Next
End Sub

Dim rnd As New Random()

Public usedStrings As New List(Of String)()

Public Function GetNextString() As String

Dim theString As String = GetRandomString()

While usedStrings.Contains(theString)
theString = GetRandomString()
End While

usedStrings.Add(theString)
Return theString

End Function

Public Function GetRandomString() As String
Dim rndString As String = ""

For i As Integer = 0 To 5
If rnd.Next(0, 2) = 0 Then
rndString += Microsoft.VisualBasic.Chr(rnd.Next(65,
91))
Else
rndString += Microsoft.VisualBasic.Chr(rnd.Next(48,
58))
End If
Next

Return rndString
End Function

End Module

It gets slower as it runs (as it has more strings to search in the
list), but at 6 or 7 strings a minute it would take weeks before the
pause would cause a problem.

Thanks,

Seth Rowe- Ocultar texto de la cita -

- Mostrar texto de la cita -

thanks. I'll try your module an see if it reaches the speed needed (I
wrote 6/7 string per minute but it's per second)
Anyway, the problem for this is that the sequences are random so I'd
have to store them (memory or database) to check if the new sequence
is a repeated one.

Thanks,
Marc
 
R

rowe_newsgroups

thanks. I'll try your module an see if it reaches the speed needed (I
wrote 6/7 string per minute but it's per second)
Anyway, the problem for this is that the sequences are random so I'd
have to store them (memory or database) to check if the new sequence
is a repeated one.

Thanks,
Marc

thanks. I'll try your module an see if it reaches the speed needed (I
wrote 6/7 string per minute but it's per second)

Slight difference :)
Anyway, the problem for this is that the sequences are random so I'd
have to store them (memory or database) to check if the new sequence
is a repeated one.

In this case they are stored in memory (the List(Of String) object)
which is much more performant than a database. This method will not be
able to keep up though - you'll need at least 1/2 million numbers each
day and I noticed significant slowdowns at 100,000. Perhaps you could
use a partially time based system - like randomly generating the first
5 digits and then making the sixth equal the hour of day it was
generated (a base 24 number). Then you would could reset the random
number list (the list of used 5 digit sequences) every hour and
significantly reduce your work load.

Thanks,

Seth Rowe
 
?

=?ISO-8859-1?Q?G=F6ran_Andersson?=

rowe_newsgroups said:
How about:

Module Module1

Public Sub Main()
For i As Integer = 0 To 100000
Console.WriteLine(GetNextString())
Next
End Sub

Dim rnd As New Random()

Public usedStrings As New List(Of String)()

Public Function GetNextString() As String

Dim theString As String = GetRandomString()

While usedStrings.Contains(theString)
theString = GetRandomString()
End While

usedStrings.Add(theString)
Return theString

End Function

Public Function GetRandomString() As String
Dim rndString As String = ""

For i As Integer = 0 To 5
If rnd.Next(0, 2) = 0 Then
rndString += Microsoft.VisualBasic.Chr(rnd.Next(65,
91))
Else
rndString += Microsoft.VisualBasic.Chr(rnd.Next(48,
58))
End If
Next

Return rndString
End Function

End Module

It gets slower as it runs (as it has more strings to search in the
list), but at 6 or 7 strings a minute it would take weeks before the
pause would cause a problem.

Thanks,

Seth Rowe

Use a dictionary instead of a list. It's much faster to find a key in a
dictionary than checking if a value exists in a list.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top