473,500 Members | 1,929 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

ID Card number generator


Hi,

I have to make a program which will generate 100,000 different ID
numbers (for example 2345-9341-0903-3432-3432 ...) It must be really
different, I meen it can not be a similar (like
2345-9341-0903-3432-343<b>1</b>) Does exist some algorithm for that...

Thanks.
*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 20 '05 #1
6 7332
On Tue, 21 Oct 2003 06:53:13 -0700, Jovo Mirkovic <yo**@sezampro.yu>
wrote:

Hi,

I have to make a program which will generate 100,000 different ID
numbers (for example 2345-9341-0903-3432-3432 ...) It must be really
different, I meen it can not be a similar (like
2345-9341-0903-3432-343<b>1</b>) Does exist some algorithm for that...

Thanks.


Consider the maximum number of IDs - 100000. That's 6 decimal digits.
Your format indicated that you want a string of four-digit numbers
separated by "-" signs. (Even if that's not the case, I'll use it to
demonstrate the principal.)

So let's say the output format is:
DDDD-DDDD-DDDD-DDDD-DDDD

We could convert our integer (the counter going from 1 to 100000) into
an ASCII string with leading zeros, and cut each character and place
it into a particular position in the output string.

e.g.
D1D2-D34D-DDDD-D5DD-D6DD
(The numbers indicate the digit positions on the input string)

The "D" digits could therefore be filled with random data. See that?

Ok, you can actually do something more useful than simply using random
data. You can use that extra bandwidth for testing the validity of the
original number.

In the example I gave, there are 14 digit spaces that we can make use
of (the D positions without the numbers in).

Taking our original number, we can then produce a hash (SHA, MD5 etc.)
of that value. Then convert it to an ASCII string (padding or cutting
the end if necessary) so that it has 14 digits. We can then insert
those digits into the empty positions. When we parse the number (and
extract the number digits) we can then apply the same hash and test
that the hash matches.

As an additional note: 14 digits for a "checksum" is a little extreme.
If we reduce that number to 6, we are left with 8 digits that are now
redundant. We can reserve those for future expansion (supporting more
than 100000 IDs). The most useful thing we can do is to introduce a
"format version code" - a single digit number that will appear in the
first position.

This leads to a well-known method of allocating IDs:

1) We need to identify the version of the ID format
2) We need to encode the original numeric value
3) We need to encode a checksum of that value

e.g.:

Example: 1162-2341-2763-1532-1623
Version: 1... .... .... .... ....
Number: .1.2 .34. .... .5.. .6..
Hash: ..6. 2..1 27.. 1... ....
Unused: .... .... ..63 ..32 1.23

We could parse that number and immediately see that it is in "format
1". We therefore know where the digits for the number and the digits
for the hash are.
i.e. Format=1 Number=123456 Hash=621271 (The rest are unused - random)

Probably more than you're after, but if you're thinking about the
format, you might as well think about its future too...

Rgds,

Nov 20 '05 #2
Hi Jovo,

You could use the random number generators in VB amd .NET.

VB gives you
Rnd

This will give you one random number at a time.

.NET gives you
System.Random
System.Security.Cryptography.RNGCryptoServiceProvi der

These two can both give you an array of random bytes which you can then
use Mod and Chr on.

This last one, despite its forbidding name is simply a very good Random
Number Generator designed for even distribution of values. (Rnd and Random are
not as rigorous)

If you are doing 100,000 Ids, I would use a StringBuilder as shown in the
code below. You might even consider creating your sbId outside the function
and reusing it by passing it in each time.

Regards,
Fergus

<code>
Imports System.Text
Imports System.Security.Cryptography

Public Function CreateId As String
Const NumDigits As Integer = 24 'Excluding the '-'

Dim oRNGCryptoServiceProvider As New RNGCryptoServiceProvider
Dim aBytes (NumDigits) As Byte
oRNGCryptoServiceProvider.GetBytes (aBytes)

Dim sbId As New StringBuilder (NumDigits * 2) 'More than enough.
Dim I As Integer

'Create group of four digits with a '-' between.
For I = 1 To NumDigits
sbId.Append (Chr (Asc("0") + aBytes(I) Mod 10))
If (I Mod 4) = 0 Then _
sbId.Append ("-")
Next

sbId.Remove (sbId.Length - 1, 1) 'Remove the trailing '-'.

Return sbId.ToString
End Function
</code>
Nov 20 '05 #3


Thanks. It was very useful to me...

Jovo

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 20 '05 #4
On Tue, 21 Oct 2003 19:02:42 +0100, "Fergus Cooney" <fi*****@post.com>
wrote:
Hi Jovo,

You could use the random number generators in VB amd .NET.

VB gives you
Rnd

This will give you one random number at a time.

.NET gives you
System.Random
System.Security.Cryptography.RNGCryptoServiceProvi der

These two can both give you an array of random bytes which you can then
use Mod and Chr on.

This last one, despite its forbidding name is simply a very good Random
Number Generator designed for even distribution of values. (Rnd and Random are
not as rigorous)

If you are doing 100,000 Ids, I would use a StringBuilder as shown in the
code below. You might even consider creating your sbId outside the function
and reusing it by passing it in each time.

Regards,
Fergus

<code>
Imports System.Text
Imports System.Security.Cryptography

Public Function CreateId As String
Const NumDigits As Integer = 24 'Excluding the '-'

Dim oRNGCryptoServiceProvider As New RNGCryptoServiceProvider
Dim aBytes (NumDigits) As Byte
oRNGCryptoServiceProvider.GetBytes (aBytes)

Dim sbId As New StringBuilder (NumDigits * 2) 'More than enough.
Dim I As Integer

'Create group of four digits with a '-' between.
For I = 1 To NumDigits
sbId.Append (Chr (Asc("0") + aBytes(I) Mod 10))
If (I Mod 4) = 0 Then _
sbId.Append ("-")
Next

sbId.Remove (sbId.Length - 1, 1) 'Remove the trailing '-'.

Return sbId.ToString
End Function
</code>


Perhaps I've missed something in MS implementation of an RNG... just
because it provides a near even distribution, you are certainly not
guaranteed that duplicates are not output - in fact, you are going to
get them. Moreover, the "mod 10" actually reduces the keyspace by a
factor of 25 for each byte. Therefore, you cannot say that this
algorithm provides the "different ID" requirement.

Rgds,
Nov 20 '05 #5
Hi Andy,

Doh!.

Quick - Add in your 000001 to 100000 bit and make a workable solution
before Jovo goes away. I'm off to bed.. ;-)

Regards,
Fergus
Nov 20 '05 #6
* "Fergus Cooney" <fi*****@post.com> scripsit:
I'm off to bed.. ;-)


;-)

--
Herfried K. Wagner
MVP · VB Classic, VB.NET
<http://www.mvps.org/dotnet>

Improve your quoting style:
<http://learn.to/quote>
<http://www.plig.net/nnq/nquote.html>
Nov 20 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

23
12904
by: JC | last post by:
I am very new to programming and learning on my own. Why do I keep getting duplicate values using this code? I want to shuffle a deck of 52 cards. The logic seems right to me. Randomize For...
10
4103
by: dries | last post by:
A friend of mine has a problem with his credit card validation routine and it is probably a simple thing to solve but I cannot find it. It has to do with the expiry dates. What happens is that as...
5
3323
by: Peteroid | last post by:
I know how to use rand() to generate random POSITIVE-INTEGER numbers. But, I'd like to generate a random DOUBLE number in the range of 0.0 to 1.0 with resolution of a double (i.e., every possible...
1
7805
by: Bill D'Innocenzo | last post by:
There are many functions available that implement credit card identification and number validation -- meaning you can decide, based on the number, if a card is a MasterCard or Visa and if the...
3
2286
by: larry | last post by:
Hi, my friends, My boss asked me to use sound card to do analog to digital conversion for my application. I know when we record sound, sound card changes anolog signal to digital file. I have no...
0
2241
by: bohuge | last post by:
Hey! At the time being I'm working on a backup solution for a Qtek9090 pocketpc, which should be able to find and backup outlook data to a server, local files, messages and contact from the sim...
0
2993
by: Limpor | last post by:
Hello, I am new to learning java, and i am trying to build the class for a calculation card game, unfortunately i can't get the public Card top() and Card takeTop() method in the Stock class. Can...
11
2457
by: Paul Furman | last post by:
I'm setting up credit card payment through authorize.net and they have the option to send a POST string back to my site once complete. I'm not sure how to proceed. They don't have much to read...
2
3329
by: rubyhuang | last post by:
the problem is A standard pack of cards can be represented as an array of 52 integers with each number representing a standard card. Thus: 0 1 2 3 4 5 6 7 8 9 10 11 12.....39 40 41 42 43 44 45 46...
0
7136
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7182
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7397
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
4923
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4611
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3106
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1430
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
672
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
316
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.