Numbers Question!

  • Thread starter Thread starter Vai2000
  • Start date Start date
V

Vai2000

what's the best way of getting unique numbers when requested other than
creating GUIDs? Right now this is what I am doing but I am not very happy
with it.


Random rnd=new Random((int)System.DateTime.Now.Ticks);
int unique=rnd.Next();

TIA
 
Hi Vai

What are these numbers for? Are they for records in a database? What is
wrong with a Guid?

Gary
 
Vai2000 said:
what's the best way of getting unique numbers when requested other than
creating GUIDs? Right now this is what I am doing but I am not very happy
with it.


Random rnd=new Random((int)System.DateTime.Now.Ticks);
int unique=rnd.Next();

TIA

--
========
Regards
Vai
========

Hi,

A random number is NOT unique! It is unpredictable (that's what "random"
means :-) ) but it is possible to get duplicate values.

Hans Kesting
 
Unfortunately I need this value for creating unique numerical ID (Its part
of a naming convention) - I don't have access to DB!!

Thanks
 
Could you please elaborate a little more? basically I don't have any
knowledge of my last number, so how can I ensure a linear number?

Thanks
PS: Ways I can think would be registry etc to maintain a starting seed and
then increment it everytime.
 
Could you please elaborate a little more? basically I don't have
any knowledge of my last number, so how can I ensure a linear
number?

Thanks
PS: Ways I can think would be registry etc to maintain a
starting seed and then increment it everytime.

If you can somehow save (or find out) the value of the last number
used in the series, then it should be simple:

number is requested
get value of most recently used number (from a data store)
increment number
store new number value in the data store
return number

Multi-user and multi-threaded access should be planned for. The
above pseudo-code should be be "locked" somehow so only one
thread/user can access it at a time. With threads, the "lock"
statement can be used. With databases, the code can be wrapped in a
transaction, or the isolation level can be set to serial access.


Chris.
 
You still haven't answered Garys original question. Whats wrong with a Guid
? This is the most reliable and easy way of generating a unique (to all
practical purposes) number on any computer without a complex error prone
tracking system.

Cheers

Doug Forster

Vai2000 said:
Could you please elaborate a little more? basically I don't have any
knowledge of my last number, so how can I ensure a linear number?

Thanks
PS: Ways I can think would be registry etc to maintain a starting seed and
then increment it everytime.
 
Well I need a number as to a GUID which is a combination of number and
alphabets!

TIA

Doug Forster said:
You still haven't answered Garys original question. Whats wrong with a Guid
? This is the most reliable and easy way of generating a unique (to all
practical purposes) number on any computer without a complex error prone
tracking system.

Cheers

Doug Forster
 
No a Guid is really a 16 byte number. You are thinking of the common hex
representation of a Guid. Use ToByteArray() and then you can represent it
however you like. Perhaps you could even 'condense' it at the risk of
reducing uniqueness.

Cheers

Doug Forster
 
Great!!
Doug Forster said:
No a Guid is really a 16 byte number. You are thinking of the common hex
representation of a Guid. Use ToByteArray() and then you can represent it
however you like. Perhaps you could even 'condense' it at the risk of
reducing uniqueness.

Cheers

Doug Forster
 
You can also use a SecureRandom to generate better random numbers. Decide
how often you are willing to have numbers collide, on average, and take
enough bits to ensure this.

If you aren't willing to have any collisions ever, you need to add a check
for uniqueness.
 
Back
Top