Solved

How to generate a random unique 10 digit alphanumeric?

Posted on 2009-04-08
10
1,572 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
  • 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
 

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
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Normally the drop down box control found in the .Net framework tools is able to select just one data and value at a time, which is displayed on the text area.   But what if you want to have multiple values to be selected in the drop down box? As …
Recently while returning home from work my wife (another .NET developer) was murmuring something. On further poking she said that she has been assigned a task where she has to serialize and deserialize objects and she is afraid of serialization. Wha…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

762 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now