Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Randomise Placement of Variables in Outputted String

  1. #1
    Junior Member
    Join Date
    Jul 2014
    Posts
    29
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Default Randomise Placement of Variables in Outputted String

    public class PasswordGen {
     
    	public static void main (String[] args) {
     
    		Generator();
     
    	}
     
    		public static void Generator() {
     
    		char[] alphabet = "abcdefghijklmnopqrstuvwxyz".toCharArray();
     
    		int w = (int) ((Math.random() * 25 +1));
    		int x = (int) ((Math.random() * 25 +1));
    		int y = (int) ((Math.random() * 25 +1));
    		int z = (int) ((Math.random() * 25 +1));
     
    		char place1 = (alphabet[w]);
    		char place2 = (alphabet[x]);
    		char place3 = (alphabet[y]);
    		char place4 = (alphabet[z]);
     
    		char[] numgen = "0123456789".toCharArray();
     
    		int a = (int) ((Math.random() * 9 + 1));
    		int b = (int) ((Math.random() * 9 + 1)); 
    		int c = (int) ((Math.random() * 9 + 1)); 
    		int d = (int) ((Math.random() * 9 + 1)); 
     
    		char place5 = (numgen[a]);
    		char place6 = (numgen[b]);
    		char place7 = (numgen[c]);
    		char place8 = (numgen[d]);
     
    		char[] symgen = "!@$%^&*_".toCharArray();
     
    		int sym1 = (int) ((Math.random() * 8 + 1));
    		int sym2 = (int) ((Math.random() * 8 + 1)); 
     
     
    		char placex = (symgen[sym1]);
    		char placey = (symgen[sym2]);
     
     
    		StringBuilder sb = new StringBuilder();
     
    			sb.append(placey);
    			sb.append(place1);
    			sb.append(place8);
    			sb.append(place2);
    			sb.append(place7);
    				String passcode1 = sb.toString();
     
    		StringBuilder se = new StringBuilder();		
     
    			se.append(place3);
    			se.append(place6);
    			se.append(placex);
    			se.append(place4);
    			se.append(place5);
    				String passcode2 = se.toString();
     
     
     
    			if (Math.random() * 2 + 1 <= 2) {
     
    				passcode1  = passcode1.toUpperCase(); }
     
    			else {
     
    				passcode2 = passcode2.toUpperCase(); }
     
     
    			System.out.print(passcode1);
    			System.out.print(passcode2);
     
     
    	}
    }


    Hi there,

    I'm trying to make a random password generator, but it isn't as random as I'd like it to be. I have set places where either a number, letter or symbol can go. I'd like to randomise the placement of these set types in the output from "symbol + letter + number + letter etc" to "? + ? + ?". I hope that makes sense. Sample output from this code:

    @W1L5m8%j4
    ^Y2L8z4@d1
    D6X5i3^i8


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,264
    Thanks
    56
    Thanked 2,372 Times in 2,343 Posts

    Default Re: Randomise Placement of Variables in Outputted String

    One way would be to have a filter String that has a letter that describes what type of character goes next. Have a method to generate each type. Have switch statement inside a loop that goes through each letter in the filter, calls the method for the next character and concatenates them.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Jul 2014
    Posts
    29
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Default Re: Randomise Placement of Variables in Outputted String

    Thanks for the suggestions but I think that it might be beyond my capabilities right now - I understand the logic but not how to implement it.

  4. #4
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    6,287
    My Mood
    Mellow
    Thanks
    252
    Thanked 777 Times in 762 Posts

    Default Re: Randomise Placement of Variables in Outputted String

    Similar to Norm's suggestion:

    1. Generate the necessary number of symbols, letters, and numbers
    2. Pass them to a method that uses a StringBuilder object and 'randomized' branching logic to add them to the StringBuilder object in different orders
    3. Return the resulting StringBuilder object as the password

    The 'randomized branching logic' that comes first to mind is a switch statement with a randomly chosen expression, and int probably. Each switch case builds the StringBuilder object from the symbols, letters, and numbers in different orders. The number of cases could be quite large, but there are likely clever ways to combine or cascade the cases to reduce the number, and in the end, it doesn't really matter. Yes, it may take time to type them all out, but ultimately, only one of them may be executed.

    If you haven't learned the switch statement yet, refer to this tutorial.

Similar Threads

  1. Trouble using Constructors and string to string() to print variables to GUI
    By pat4prez in forum What's Wrong With My Code?
    Replies: 2
    Last Post: January 27th, 2014, 06:22 AM
  2. [SOLVED] Sorting an object array using string variables from a string array
    By Shadud in forum What's Wrong With My Code?
    Replies: 0
    Last Post: November 26th, 2012, 05:50 PM
  3. JLabel placement issues
    By fride360 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 11th, 2011, 01:20 PM
  4. How to define a string pattern using variables?
    By ice in forum What's Wrong With My Code?
    Replies: 8
    Last Post: January 7th, 2011, 10:45 PM