Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Random Unique Serial Number Generator...

1 view
Skip to first unread message

Mel

unread,
Sep 13, 2003, 11:24:01 AM9/13/03
to
i need to create random unique serial numbers, say 10 chars + digits
combined

can anyone help ?


Georgios Petasis

unread,
Sep 13, 2003, 12:31:44 PM9/13/03
to
set char_set {0 1 2 3 4 5 6 7 8 9 A B C D E F G H I J K a b c d e f g . . .}
set len [llength $char_set]
set serial {}
for {set i 0} {$i < 10} {incr i} {
set index [expr {int(rand()*$len)}]
append serial [lindex $char_set $index]
}
puts $serial

George


"Mel" <m...@redolive.com> wrote in message
news:rEG8b.5$B65....@news.uswest.net...

Mark Bessey

unread,
Sep 13, 2003, 2:58:33 PM9/13/03
to
"Mel" <m...@redolive.com> wrote in message
news:rEG8b.5$B65....@news.uswest.net...
> i need to create random unique serial numbers, say 10 chars + digits
> combined
>
> can anyone help ?

You do realize that these two requirements are incompatible, right? Random
numbers won't necessarily be unique. You'll need to keep a list of all the
serial numbers you've used so far, then check each (pseudo-)random new
serial number against the list.

I'm not sure why you'd want a random number for a serial number, anyway.
Most serial numbers used in the real world actually have useful information
coded into themm - like the date of manufacture for hardware. This comes in
handy when you need to do a recall later ;-)

-Mark


Wojciech Kocjan

unread,
Sep 13, 2003, 4:01:43 PM9/13/03
to
Mel wrote:

> i need to create random unique serial numbers, say 10 chars + digits
> combined

Basically, I use the following:

format %08x%08x%s [clock seconds] [clock clicks] [md5::md5 $hash]

hash is some unique string identifying a machine - for example in a
client connecting to a server, I do hash from some random string and all
the usernames/hostnames.

It is not random, but unique (although it is 48 bytes - though you could
use base64 instead of hex and get 32 bytes).

--
WK

0 new messages