Results 1 to 12 of 12
Like Tree3Likes
  • 1 Post By fakepics500
  • 1 Post By Dark
  • 1 Post By Dark

Thread: Random Password

  1. #1
    qwerty53's Avatar
    qwerty53 is offline Member
    Join Date
    Jul 2011
    Posts
    32
    Rep Power
    0

    Question Random Password

    maxlenght must be 8 characters. "0123456789abcdefghijklmnopqrstuvwxyz"How to generate random password ? and it must be everytime different when you run it again.
    Last edited by JosAH; 07-11-2011 at 09:36 AM. Reason: removed the boldface and other font juggling

  2. #2
    Dark's Avatar
    Dark is offline Senior Member
    Join Date
    Apr 2011
    Location
    Camp Lejuene, North Carolina
    Posts
    643
    Rep Power
    4

    Default

    Whoa buddy, getting pretty wild with that text there.

    There are plenty of ways you can do it. The easiest would be to make an array containing all of these characters and then picking a random index to make it. If you use the Random class, you will get a different combo every time. You could probably accomplish this with ~10 lines of code.
    • Use [code][/code] tags when posting code. That way people don't want to stab their eyes out when trying to help you.
    • +Rep people for helpful posts.

  3. #3
    qwerty53's Avatar
    qwerty53 is offline Member
    Join Date
    Jul 2011
    Posts
    32
    Rep Power
    0

    Default

    Can you show me a simple code about random function. I don't how to generate different password everytime...

  4. #4
    Dark's Avatar
    Dark is offline Senior Member
    Join Date
    Apr 2011
    Location
    Camp Lejuene, North Carolina
    Posts
    643
    Rep Power
    4

    Default

    Google is a good source to figure out how to use something. There are thousands of examples of how to use different parts of Java on the web.

    Read these articles on the Random class. It will teach you how to use it.
    Java Random Numbers
    Java Practices -> Generate random numbers
    • Use [code][/code] tags when posting code. That way people don't want to stab their eyes out when trying to help you.
    • +Rep people for helpful posts.

  5. #5
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,513
    Rep Power
    25

    Default

    how to generate different password everytime
    That impiles that you keep a record of what has been generated in the past so you can reject a newly generated one if it has already been generated and keep going back and getting another one until it is unique.

  6. #6
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    If you know how to generate a password once, it shouldn't be too hard to keep track of previous password generations. It may be a little different(not much) if you want different executions to not generate the same passwords. Try building it incrementally, first generate a password, then figure out how to track previous passwords, then find out how to save the passwords. A file would probably be the easiest.

  7. #7
    fakepics500 is offline Member
    Join Date
    Jul 2011
    Posts
    43
    Rep Power
    0

    Default

    Java Code:
    import java.util.*;
    class Pass
    {
    	public static void main(String ar[])
    	{
    		Random r= new Random();
    		 String arr[]={"1","2","3","4","5","6","7","8","9","0","a","b","c"};
    		int p;
    		for (int i=1;i<=8;i++)
    		{
    			p=r.nextInt(13);
    			System.out.print(arr[p]);
    		}
    	}
    }
    qwerty53 likes this.

  8. #8
    Dark's Avatar
    Dark is offline Senior Member
    Join Date
    Apr 2011
    Location
    Camp Lejuene, North Carolina
    Posts
    643
    Rep Power
    4

    Default

    Quote Originally Posted by fakepics500 View Post
    Java Code:
    import java.util.*;
    class Pass
    {
    	public static void main(String ar[])
    	{
    		Random r= new Random();
    		 String arr[]={"1","2","3","4","5","6","7","8","9","0","a","b","c"};
    		int p;
    		for (int i=1;i<=8;i++)
    		{
    			p=r.nextInt(13);
    			System.out.print(arr[p]);
    		}
    	}
    }
    This is spoon feeding, not only that but your program isn't very flexible. You also hard coded your random integer instead of using arr.length making the use limited and if the variables ever change, you have to go and change the number of possible choices.

    The method you decided to take is also inefficient, as instead of using an integer and storing your random choice to it you should have done something like System.out.print(arr[r.nextInt(arr.length)]);

    Thanks to your spoon feeding, the OP no longer has to think for himself and is probably passing your code off as his homework. Good Job.
    sunde887 likes this.
    • Use [code][/code] tags when posting code. That way people don't want to stab their eyes out when trying to help you.
    • +Rep people for helpful posts.

  9. #9
    fakepics500 is offline Member
    Join Date
    Jul 2011
    Posts
    43
    Rep Power
    0

    Default

    Quote Originally Posted by Dark View Post
    You also hard coded your random integer instead of using arr.length making the use limited and if the variables ever change, you have to go and change the number of possible choices.

    The method you decided to take is also inefficient, as instead of using an integer and storing your random choice to it you should have done something like System.out.print(arr[r.nextInt(arr.length)]);
    thanks for pointing out my mistakes

    Thanks to your spoon feeding, the OP no longer has to think for himself and is probably passing your code off as his homework. Good Job.
    i am sorry for spoon feeding i am also new to java.i was just trying to solve the problem so that i could also get to learn something

  10. #10
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default

    i am sorry for spoon feeding i am also new to java.i was just trying to solve the problem so that i could also get to learn something
    There is no problem with that but part of the learning process doesn't involve you posting your solution on the net for others to cheat from.

  11. #11
    bigsonny is offline Senior Member
    Join Date
    Nov 2010
    Posts
    164
    Rep Power
    4

    Default

    Boom roasted! Just kidding!

  12. #12
    Dark's Avatar
    Dark is offline Senior Member
    Join Date
    Apr 2011
    Location
    Camp Lejuene, North Carolina
    Posts
    643
    Rep Power
    4

    Default

    Quote Originally Posted by Junky View Post
    There is no problem with that but part of the learning process doesn't involve you posting your solution on the net for others to cheat from.
    Exactly, if you are trying to solve problems that other people have for learning purposes there is no shame in trying to make the program. However, posting the solution doesn't help the user. If you manage to figure out how to solve the problem, that means you know roughly what the OP should do so if he has any more problems with his code you are better equipped to give him suggestions.
    Norm likes this.
    • Use [code][/code] tags when posting code. That way people don't want to stab their eyes out when trying to help you.
    • +Rep people for helpful posts.

Similar Threads

  1. Replies: 14
    Last Post: 10-19-2009, 10:57 AM
  2. Replies: 8
    Last Post: 04-19-2009, 05:50 PM
  3. how to check password for 3 times enterd wrong password
    By sk.mahaboobbhasha@gmail.c in forum New To Java
    Replies: 2
    Last Post: 11-14-2008, 07:53 PM
  4. how to check password for 3 times enterd wrong password
    By sk.mahaboobbhasha@gmail.c in forum Java Servlet
    Replies: 0
    Last Post: 11-14-2008, 01:22 PM
  5. How to check password of a jsp/html with the password of Database(mysql) #1
    By sk.mahaboobbhasha@gmail.c in forum Java Servlet
    Replies: 2
    Last Post: 11-14-2008, 01:11 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •