Advertisement
Guest User

Random String Generator

a guest
Jul 1st, 2015
260
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.73 KB | None | 0 0
  1.             Random rn = new Random();
  2.             // This is the random class, it just declares rn being a new random
  3.  
  4.             char[] poolOfCharacters = "qwertyuiopasdfghjklzxcvbnm1234567890QWERTYUIOPASDFGHJKLZXCVBNM".ToCharArray();
  5.             // Creates a character array which is equal to a string of all the characters you want, and then it is converted to a character array.
  6.             // The program will choose a random character from here x many times
  7.  
  8.             int x = 10;
  9.             // The amount of times you want it to repeat itself (so, the length of the final string)
  10.  
  11.             string finalResult = string.Empty;
  12.             // Declares a new empty string where we will then store the final string in.
  13.  
  14.             for (int i = 0; i <= x; i++)
  15.                 // This is basically just saying, "i" is 0, and until "i" reaches the same as x, keep doing what is in these brackets, and each time it finishes the bracket, add 1 to "i"
  16.             {
  17.                 int character = rn.Next(0, poolOfCharacters.Length);
  18.                 // Uses the random class to select a random number between 0 and the length of the character array (so 62)
  19.                 // Overall: selects a random number between 0 and 62
  20.  
  21.                 finalResult += poolOfCharacters[character];
  22.                 // Add whatever is already in the final string to a random character specified by the number we created earlier
  23.                 // The number says which character it will be, if we did:
  24.                 // poolOfCharacters[0], that would give us the character "q", the first one in the list.
  25.             }
  26.  
  27.             MessageBox.Show(finalResult);
  28.             // After this loop is finished, it shows the final string in a messagebox
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement