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 ..
Printable View
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 ..
Digits should be from 0 to 9, or also include characters A - Z?
it shud be numeric..
i.e from 0 to 9
does a key that is generated stored somewhere?
where exactly do you need such a large number for?
I'd suggest using a random generator and simply logging the keys that you have used to keep them unique...
something like:
the 'keys' list would then be written to a file on exit and loaded on load...Code:
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;
}
hope it helps...
I want to create registration keys of 10 digit long which will be unique and these nos will be stored into an excel file
Quote:
Originally Posted by connect_fc
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....)
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)
'Quote:
Originally Posted by foamy
int.MaxValue == 2,147,483,647. Thus you are only using a small range of the digit key
I would suggest
if you want the key to be greater than 10 digits jsut change the secondKeyPart generation into somethinf biggerCode: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;
}
Code:// last 'n' numbers, in this case 4
keySecondPart = ran.Next(1000, 9999);
//you total key is nog 12 digit
EDITED: I suck with a calculator
Talk about deliberately wasting resources....Quote:
Code:result = long.Parse(keyFirstPart.ToString() + keySecondPart.ToString());
Just make the two variables long, and do a quick multiply and add!
Or...
If you need it in a different format, just convert. Otherwise, you can actually use zeros as starting digits.Code:
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();
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.
Yeah, that did strike me as I was writing it, but I figured it would be a nice starting point... Anyway, nice solution :)Quote:
Originally Posted by dannystommen