Click to See Complete Forum and Search --> : Generate Unique Number


sunny57344
February 22nd, 2006, 12:03 PM
Hi,
I want to generate a unique number with the help of two numbers.

For ex: suppose the numbers are 6 and 17.
For this combination, i should always get the same unique number.
How can I do this in C#?




Regards:
Sunny

boudino
February 23rd, 2006, 03:26 AM
Use random class (see http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfSystemRandomClassTopic.asp).

Every PRNG (Pseudo Random Number Generator) gives you the same sequence of numbers if you initialize it with the same seed. You have to only decide how to derive seed from your values.

int i = new Random(6*100 + 17).Next();
int j = new Random(6*100 + 17).Next();
// i == j is true now

jmcilhinney
February 23rd, 2006, 03:41 AM
In what context are these numbers to be unique, because seeding a random object implies nothing about uniqueness?

boudino
February 23rd, 2006, 04:52 AM
I have re-readed original post and I have to tell - I am totaly out. I don't know why I've posted anything about PRNG (althought I am right in my post). :)

Back to original question: I think that you don't need realy unique number (if so, you could use GUID or some feature of RBDMS), but it seems to me that you are interesting in something like has code. You simply combine with xor operator (6 ^ 17) or develop more sofisticated solution. I think the best will be tell us purpose of you need, than we could be more specific.

exterminator
February 23rd, 2006, 09:29 AM
Are the random numbers to be between 6 and 17? How are 6 and 17 important? Regards.

K7SN
February 23rd, 2006, 12:40 PM
You want to generate a unique hashed number. Unless you have some sort of finite bounds that may not be possible. By finite bounds I mean that the ranges of both the initial numbers are bound, then the unique number can be represented in a set of numbers bound by the product of the ranges of the two initial numbers. Simple example: The range of the first number to hash is 0 to 20, the range of the second number to hash is 10 to 90, every combination can be represented with with numbers from 0 to 1700.


First Digit * 81 + second digit - 10 = unique number.


Somehow, I suspect that like boudino, I have not fully comprehended what you are trying to do :)