-5

I'm creating a random password generator in java and need some help with how I can generate random char values in the program. Any ideas? I'm using a menu system as a way to show different types of passwords to generate like lowercase, uppercase, etc.

Any advice would help.

Thanks!

eddie
  • 1,252
  • 3
  • 15
  • 20
user2821628
  • 21
  • 1
  • 3
  • http://www.cs.geneseo.edu/~baldwin/reference/random.html , http://stackoverflow.com/questions/363681/generating-random-numbers-in-a-range-with-java & lookup import java.util.Random; – Caffeinated Oct 06 '13 at 20:55
  • 2
    Solutions to this type of problem have been shown elsewhere, search for something before asking. If you can't find it, try something. If you can't get it to work, then ask. – sage88 Oct 06 '13 at 21:02
  • Why this question was so strongly down voted? There are other duplicates and there was no such a punishment? I think sometimes people just get insane when down voting. Especially that down voting questions of new comers bans them and do not teach anything. Please down vote judiciously. – Seweryn Habdank-Wojewódzki Jun 23 '17 at 07:51

2 Answers2

2
Random r = new Random();

String alphabet = "123xyz";

// Prints 50 random characters from alphabet
for (int i = 0; i < 50; i++) 
{
    System.out.println(alphabet.charAt(r.nextInt(alphabet.length())));
} 

Instead of printing your chars, add them to a StringBuilder, and you have your random password.

(Source)

Community
  • 1
  • 1
Georgian
  • 8,795
  • 8
  • 46
  • 87
-1

Here's another solution:

    public int genRandomNumber(int min, int max)
    {
        return new Random().nextInt((max - min) + 1) + min;
    }

    public String genRandomPassword(int length)
    {
        String pass = "";

        for (int i = 0; i < length; i++)
        {
            pass += this.genRandomNumber(0x61, 0x7A);//min and max are ASCII char values.
        }

        return pass;
    }
Rohan
  • 359
  • 2
  • 16