Solved

How to generate a random unique 10 digit alphanumeric?

Posted on 2009-04-08
10
1,666 Views
Last Modified: 2013-12-17
Hello Experts,
Could anybody please post me a code snippet to generate a random and unique 10 digit alphanumeric in C#? I need to insert this 10 digit alphanumeric into a primary key column of a table. Any help is greatly appreciated! Thanks!
0
Comment
Question by:ipjyo
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 3
10 Comments
 
LVL 41

Expert Comment

by:guru_sami
ID: 24102351
0
 
LVL 6

Accepted Solution

by:
SaedSalman earned 500 total points
ID: 24102354
Hello
change the MaxSize, MinSize, string 'a' (allowed symboles) to fit your need,
also follow-up link here:
http://www.experts-exchange.com/Programming/Languages/C_Sharp/Q_24001435.html

regrads
private string GetUniqueKey()
  {
  int maxSize  = 10 ;
  int minSize = 10 ;
  char[] chars = new char[62];
  string a;
  a = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";
  chars = a.ToCharArray();
  int size  = maxSize ;
  byte[] data = new byte[1];
  RNGCryptoServiceProvider  crypto = new RNGCryptoServiceProvider();
  crypto.GetNonZeroBytes(data) ;
  size =  maxSize ;
  data = new byte[size];
  crypto.GetNonZeroBytes(data);
  StringBuilder result = new StringBuilder(size) ;
  foreach(byte b in data )
  { result.Append(chars1)>); }
   return result.ToString();
  }

Open in new window

0
 

Author Comment

by:ipjyo
ID: 24102403
Thanks for the codes..I will try them and let you know soon.

Thanks!
0
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 

Author Comment

by:ipjyo
ID: 24102489
Hi,
First I am trying the below code. Could you tell me if I need to import any namespace to support RNGCryptoServiceProvider and its methods? because my visual studio is not suppoerting this class.
Can you please clarify?
And also I am not able to understand the following line. Thanks!
result.Append(chars1)>);  
private string GetUniqueKey()
  {
  int maxSize  = 10 ;
  int minSize = 10 ;
  char[] chars = new char[62];
  string a;
  a = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";
  chars = a.ToCharArray();
  int size  = maxSize ;
  byte[] data = new byte[1];
  RNGCryptoServiceProvider  crypto = new RNGCryptoServiceProvider();
  crypto.GetNonZeroBytes(data) ;
  size =  maxSize ;
  data = new byte[size];
  crypto.GetNonZeroBytes(data);
  StringBuilder result = new StringBuilder(size) ;
  foreach(byte b in data )
  { result.Append(chars1)>); }
   return result.ToString();
  }

Open in new window

0
 

Author Comment

by:ipjyo
ID: 24102531
I have added System.Security.Cryptography..now it is showing.
But I still dont get if there is any typo error in this
result.Append(chars1)>);  

Thank you!
0
 

Author Comment

by:ipjyo
ID: 24102613
I tried to give
result.Append(chars1);
I have got an error like "there is no chars1 variable defined". When I tried to give it as
result.Append(chars);
it generated a long string as shown below.
Could you please help me what I have missed here.
Thanks!


abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890

Open in new window

0
 
LVL 6

Assisted Solution

by:SaedSalman
SaedSalman earned 500 total points
ID: 24102760
Sorry, But I logged out.
Pre-Tested
private string GetUniqueKey()
  {
  int maxSize  = 10 ;
  int minSize = 10 ;
  char[] chars = new char[62];
  string a;
  a = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";
  chars = a.ToCharArray();
  int size  = maxSize ;
  byte[] data = new byte[1];
  RNGCryptoServiceProvider  crypto = new RNGCryptoServiceProvider();
  crypto.GetNonZeroBytes(data) ;
  size =  maxSize ;
  data = new byte[size];
  crypto.GetNonZeroBytes(data);
  StringBuilder result = new StringBuilder(size) ;
  foreach(byte b in data )
  { result.Append(chars[b % (chars.Length - 1)]); }
   return result.ToString();
  }

Open in new window

0
 

Author Comment

by:ipjyo
ID: 24102761
The code "http://snipplr.com/view/13357/random-string-generator" is working fine.
Thanks very much guru-sami
0
 

Author Comment

by:ipjyo
ID: 24102775
Hi SaedSalman,

Thank you for working for me..I would also like to save and use your code.
0
 
LVL 6

Expert Comment

by:SaedSalman
ID: 24102825
You are welcom,
> I would also like to save and use your code.
actually not mine ;-)

http://www.codeproject.com/KB/aspnet/UniqueKeys.aspx?display=PrintAll&fid=315463&df=90&mpp=25&noise=3&sort=Position&view=Quick&select=2493319


Best Regards,
Saed Salman
0

Featured Post

[Webinar] Learn How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.

628 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question