Click to See Complete Forum and Search --> : generate 10-digit random unique numeric keys.


connect_fc
October 14th, 2008, 08:00 AM
How can I generate 10-digit random unique numeric keys..
Can anybody suggest me an algorithm which is easy to implement or any other work around ..

cilu
October 14th, 2008, 08:02 AM
Digits should be from 0 to 9, or also include characters A - Z?

connect_fc
October 14th, 2008, 08:37 AM
it shud be numeric..
i.e from 0 to 9

dannystommen
October 14th, 2008, 08:49 AM
does a key that is generated stored somewhere?

where exactly do you need such a large number for?

foamy
October 14th, 2008, 08:50 AM
I'd suggest using a random generator and simply logging the keys that you have used to keep them unique...

something like:



List<int> keys = new List<int>();

private int NewKey()
{
Random ran = new Random(DateTime.Now.Millisecond);

int key = 0;
do
{
key = ran.Next(1000000000, int.MaxValue);

} while (keys.Contains(key));

keys.Add(key);
return key;
}


the 'keys' list would then be written to a file on exit and loaded on load...

hope it helps...

connect_fc
October 14th, 2008, 08:56 AM
I want to create registration keys of 10 digit long which will be unique and these nos will be stored into an excel file

TheCPUWizard
October 14th, 2008, 08:57 AM
I want to create registration keys of 10 digit long which will be unique and these nos will be stored into an excel file


Why not just use a GUID? [not a 10 digit number, but guaranteed to never duplicate, and trivial to generate, and no need to keep track of all generated items (although for validation you will want that....)

connect_fc
October 14th, 2008, 09:07 AM
I guess GUID will give me alphanumeric keys which I dont want..
I need numeric 10 digit reg key (in future it can be more than 10 & less than 15 also)

dannystommen
October 14th, 2008, 09:22 AM
I'd suggest using a random generator and simply logging the keys that you have used to keep them unique...

something like:



List<int> keys = new List<int>();

private int NewKey()
{
Random ran = new Random(DateTime.Now.Millisecond);

int key = 0;
do
{
key = ran.Next(1000000000, int.MaxValue);

} while (keys.Contains(key));

keys.Add(key);
return key;
}


the 'keys' list would then be written to a file on exit and loaded on load...

hope it helps...'

int.MaxValue == 2,147,483,647. Thus you are only using a small range of the digit key

I would suggest

List<long> keys = new List<long>();

private long NewKey() {
Random ran = new Random(DateTime.Now.Millisecond);

int keyFirstPart = 0;
int keySecondPart = 0;
long result = 0;
do {
// first 8 numbers
keyFirstPart = ran.Next(10000000, 99999999);
// last 'n' numbers, in this case 2, easy to make it bigger up to 7
keySecondPart = ran.Next(10, 99);

//total key of 10 digits
result = long.Parse(keyFirstPart.ToString() + keySecondPart.ToString());
}
while (keys.Contains(result));

keys.Add(result);
return result;
}


if you want the key to be greater than 10 digits jsut change the secondKeyPart generation into somethinf bigger

// last 'n' numbers, in this case 4
keySecondPart = ran.Next(1000, 9999);

//you total key is nog 12 digit

eclipsed4utoo
October 14th, 2008, 09:54 AM
EDITED: I suck with a calculator

TheCPUWizard
October 14th, 2008, 09:56 AM
result = long.Parse(keyFirstPart.ToString() + keySecondPart.ToString());



Talk about deliberately wasting resources....

Just make the two variables long, and do a quick multiply and add!

mmetzger
October 14th, 2008, 10:03 AM
Or...



int maxdigits = 10; // Change to needed # of digits
StringBuilder result = new StringBuilder();
Random r = new Random(); // Seed with what you feel is appropriate
for (int i = 0; i<=maxdigits; i++)
{
result.Append(r.Next(10)); // Append a number from 0 to 9
}

string key = result.ToString();


If you need it in a different format, just convert. Otherwise, you can actually use zeros as starting digits.

Now storing this in excel will require making sure the data format is correct or you'd lose the leading zeros.

Just some advice from experience - use a GUID. Or a hash. Or something more complex. Depending on what you're trying to create, it's pretty simple for someone to brute force this type of identifier by simply looping through the possibilities.

foamy
October 14th, 2008, 02:19 PM
'

int.MaxValue == 2,147,483,647. Thus you are only using a small range of the digit key

I would suggest

List<long> keys = new List<long>();

private long NewKey() {
Random ran = new Random(DateTime.Now.Millisecond);

int keyFirstPart = 0;
int keySecondPart = 0;
long result = 0;
do {
// first 8 numbers
keyFirstPart = ran.Next(10000000, 99999999);
// last 'n' numbers, in this case 2, easy to make it bigger up to 7
keySecondPart = ran.Next(10, 99);

//total key of 10 digits
result = long.Parse(keyFirstPart.ToString() + keySecondPart.ToString());
}
while (keys.Contains(result));

keys.Add(result);
return result;
}


if you want the key to be greater than 10 digits jsut change the secondKeyPart generation into somethinf bigger

// last 'n' numbers, in this case 4
keySecondPart = ran.Next(1000, 9999);

//you total key is nog 12 digit


Yeah, that did strike me as I was writing it, but I figured it would be a nice starting point... Anyway, nice solution :)