Advertisement
Guest User

RandomGenerator

a guest
Sep 11th, 2014
481
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.46 KB | None | 0 0
  1. import java.util.ArrayList;
  2. /*
  3.  * File Name: RandomGenerator.java
  4.  * Package Name: N/A
  5.  * Application Name: Random String Generator
  6.  *
  7.  * Version Info: 1.0.0
  8.  * Version Date: 09-09-2014
  9.  *
  10.  * Creator: Corey D. Eacret
  11.  * Website: http://www.appdappdev.com
  12.  * Contact: appdapp@gmail.com
  13.  *
  14.  * Description:
  15.  * This is the class for the Random String Generator that actually creates the random
  16.  * strings. It contains 3 nested classes that produce a random single digit integer,
  17.  * random upper or lower case alpha character, or one of a list of approved symbols.
  18.  * The class will output an ArrayList with each index containing one of the randomly
  19.  * generated characters.
  20.  *
  21.  */
  22.  
  23. public class RandomGenerator {
  24.    
  25.     // We start by declaring the length and type variables, which we get from user input
  26.     // later on in the program.
  27.     char randType = 'a';
  28.     int randLength = 0;
  29.    
  30.     // This is our first nested class, the one that produces a random number.
  31.     class numGen {
  32.         int rNum() {
  33.             // The actual random function, gets a number between 0 and 9.
  34.             int rN = (int) (Math.random() * 10);
  35.             return rN;
  36.         }
  37.     }
  38.    
  39.     // This is our second nested class, this one produces a random alpha character.
  40.     class alphaGen {
  41.         char rAlpha() {
  42.             // We start by getting a random number, between 0 and 51.
  43.             int rN = (int) (Math.random() * 52);
  44.             // We then use that number to pick a random starting point, 'A' or 'a'.
  45.             char base = (rN < 26) ? 'A':'a';
  46.             // Finally, we use our starting point, and random number, to pick our alpha char.
  47.             char rA = (char) (base + rN % 26);
  48.             return rA;
  49.         }
  50.     }
  51.    
  52.     // Our final nested class, this one uses a list of symbols, and chooses a random one.
  53.     class symGen {
  54.         char rSymbol() {
  55.             // The symbol list, this allows us to only use a select few symbols.
  56.             char[] symList = {'!','@','#','$','%','^','&','*','(',')','-','_','=','+',',','.','<','>','/','?',';',':','`','~'};
  57.             // We get our random value, a number between 0, and the amount of symbols.
  58.             int rN = (int) (Math.random() * 24);
  59.             // Finally, we use our random number to pick one of the symbols from the list.
  60.             char rS = symList[rN];
  61.             return rS;
  62.         }
  63.     }
  64.    
  65.     // Now we create a new object for each of our random generators.
  66.     RandomGenerator.numGen getRandNum = new RandomGenerator.numGen();
  67.     RandomGenerator.alphaGen getRandAlpha = new RandomGenerator.alphaGen();
  68.     RandomGenerator.symGen getRandSym = new RandomGenerator.symGen();
  69.    
  70.     // This is the method that takes the user input, and then creates the random string.
  71.     public ArrayList<String> randGenerator(int randType, int randLength) {
  72.        
  73.         // Our ArrayList which is the output at the end of this class.
  74.         ArrayList<String> outputString = new ArrayList<String>();
  75.        
  76.         // Start of our if else list, that uses the user input to select the type of string.
  77.         if (randType == 'a' || randType == 'A') {
  78.            
  79.             // For statement to generate a random character for the length requested.
  80.             for (int aType=0;aType!=randLength;aType++) {
  81.                
  82.                 // We use the random number generator to also create a random number that
  83.                 // we use to decide which type (num,alpha,sym) for each spot.
  84.                 String rndOutput;
  85.                 int whichType = getRandNum.rNum();
  86.                
  87.                 if (whichType < 4) {
  88.                     // Here we get a random alpha character, and convert char to string.
  89.                     rndOutput = Character.toString(getRandAlpha.rAlpha());
  90.                     outputString.add(rndOutput);
  91.                 }
  92.                 else if (whichType > 3 && whichType < 7) {
  93.                     // Here we get a random number, and convert int to string.
  94.                     rndOutput = Integer.toString(getRandNum.rNum());
  95.                     outputString.add(rndOutput);
  96.                 }
  97.                 else if (whichType > 6) {
  98.                     // Here we get a random symbol, and conert char to string.
  99.                     rndOutput = Character.toString(getRandSym.rSymbol());
  100.                     outputString.add(rndOutput);
  101.                 }
  102.             }
  103.         }
  104.        
  105.         // Here we get our string, if type is set to 'b'. The functions inside are the same
  106.         // as above, so there won't be comments on the rest of these.
  107.         else if (randType == 'b' || randType == 'B') {
  108.             for (int aType=0;aType!=randLength;aType++) {
  109.                
  110.                 String rndOutput;
  111.                 int whichType = getRandNum.rNum();
  112.                
  113.                 if (whichType < 5) {
  114.                     rndOutput = Character.toString(getRandAlpha.rAlpha());
  115.                     outputString.add(rndOutput);
  116.                 }
  117.                 else if (whichType > 4) {
  118.                     rndOutput = Integer.toString(getRandNum.rNum());
  119.                     outputString.add(rndOutput);
  120.                 }
  121.             }
  122.         }
  123.        
  124.         // Here we get our string for 'c' type.
  125.         else if (randType == 'c' || randType == 'C') {
  126.             for (int aType=0;aType!=randLength;aType++) {
  127.                
  128.                 String rndOutput;
  129.                
  130.                 rndOutput = Character.toString(getRandAlpha.rAlpha());
  131.                 outputString.add(rndOutput);
  132.             }
  133.         }
  134.        
  135.         // Here we get our string for 'd' type.
  136.         else if (randType == 'd' || randType == 'D') {
  137.             for (int aType=0;aType!=randLength;aType++) {
  138.                
  139.                 String rndOutput;
  140.                
  141.                 rndOutput = Integer.toString(getRandNum.rNum());
  142.                 outputString.add(rndOutput);
  143.             }
  144.         }
  145.        
  146.         // Here we get our string for 'e' type.
  147.         else if (randType == 'e' || randType == 'E' ) {
  148.             for (int aType=0;aType!=randLength;aType++) {
  149.                
  150.                 String rndOutput;
  151.                
  152.                 rndOutput = Character.toString(getRandSym.rSymbol());
  153.                 outputString.add(rndOutput);
  154.             }
  155.         }
  156.        
  157.         // Even with error checking, we leave this in just in case funny things happen.
  158.         else {
  159.             System.out.println("INVALID TYPE!!");
  160.         }
  161.        
  162.         // Here we send out our completed random string!!
  163.         return outputString;
  164.     }
  165. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement