Numbers Question!

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
 
G

Gary van der Merwe

Hi Vai

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

Gary
 
H

Hans Kesting

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
 
V

Vai2000

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

Thanks
 
V

Vai2000

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.
 
C

Chris R. Timmons

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.
 
D

Doug Forster

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.
 
V

Vai2000

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
 
D

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
 
V

Vai2000

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
 
J

Jason Smith

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.
 

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