By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,844 Members | 1,859 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,844 IT Pros & Developers. It's quick & easy.

Selecting a fixed number of charaters from upper, lower and numeric values

P: 1
I'm trying to lean C# and struggling as where to start with problem and lead would be much appreciated

Taking a random string from a continuous string of upper and lower alphas and o to 9 number characters is alpha biased. is it possible to randomly select a fixed number of separate upper, lower alphas and numbers, concatenate the results and random shuffle that final string.
3 Weeks Ago #1
Share this Question
Share on Google+
1 Reply


Expert 100+
P: 1,025
I did read your question, you do mean something like this?:
Expand|Select|Wrap|Line Numbers
  1. namespace RandomString
  2. {
  3.     class Program
  4.     {
  5.         static void Main(string[] args)
  6.         {
  7.             String str = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
  8.             Random rnd = new Random(DateTime.Now.Millisecond);
  9.  
  10.  
  11.             // is it possible to randomly select a fixed number of separate upper, lower alphas and numbers,
  12.             int fixednumber = 10;
  13.             String[] selection = new String[fixednumber];
  14.             int i = 0;
  15.             while (i<10 )
  16.             {
  17.                 selection[i] = str.Substring(rnd.Next(str.Length), 1);
  18.                 i++;
  19.             }
  20.  
  21.             // concatenate the results
  22.             String newString = "";
  23.             foreach (String item in selection)
  24.             {
  25.                 newString = newString + item;
  26.             }
  27.             Console.WriteLine("The new string is: {0}", newString);
  28.  
  29.  
  30.             // and random shuffle that final string. 
  31.             for (int x=0; x<rnd.Next(4,10); x++)
  32.             {
  33.                 Shuffle(newString);
  34.             }
  35.             Console.WriteLine("Done random shuffling..."); 
  36.         }
  37.  
  38.         static void  Shuffle(String s)
  39.         {
  40.             // Shuffle...
  41.         }
  42.     }
  43. }
  44.  
2 Weeks Ago #2

Post your reply

Sign in to post your reply or Sign up for a free account.