0

The code I have here only manages to randomize the length, I need to randomize the string within my class createPassword. But a method does not exist that randomizes string, only integers. Anyone have any advice?

The main:

public class Program {
    public static void main(String[] args) {
        PasswordRandomizer randomizer = new PasswordRandomizer(13);
        System.out.println("Password: " + randomizer.createPassword());
        System.out.println("Password: " + randomizer.createPassword());
        System.out.println("Password: " + randomizer.createPassword());
        System.out.println("Password: " + randomizer.createPassword());
}
}

Class:

import java.util.Random;

public class PasswordRandomizer {
    private Random random = new Random();
    private int length;

    public PasswordRandomizer(int length) {
        this.length=length;
        random = new Random();
    }

    public String createPassword() {
        String alphabet = "abcdefghijklmnopqrstuvwxyz";
        int randomCharIndex = random.nextInt(length);
        String randomstr = alphabet.substring(randomCharIndex); 
        return randomstr;
    }
}

Example outputs:

Password: ghijklmnopqrstuvwxyz
Password: lmnopqrstuvwxyz
Password: jklmnopqrstuvwxyz
Password: ijklmnopqrstuvwxyz
2
  • And with an integer you can point into a character list and pick one and repeat... Commented Mar 27, 2016 at 5:17
  • if this is for production use, you should get help of some libraries.. you can look into UUID.getRandomUUID(); Commented Mar 27, 2016 at 5:18

3 Answers 3

2

This would work fine:

    public String createPassword() {
        StringBuilder pass = new StringBuilder();
        Random r = new Random();

        for(int i = 0 ; i < length ; i++) {
            pass.append( (char)('a' + r.nextInt('z'-'a')) );
        }
        return pass.toString();
    }

The idea is to exploit that char can be used as an int and converted back to a char (and that alphabetical chars are consecutive in the ASCII code). So you use this to get a random char:
minimumValue + random.nextInt(MaxValue-MinValue)

Sign up to request clarification or add additional context in comments.

6 Comments

you didn't pass length as method's parameter
@KenBekov in his sample code, it's a field of his main class
So you created a new class and object here? StringBuilder pass = new StringBuilder();. Also can you explain this line pass.append( (char)('a' + r.nextInt('z'-'a')) );
@Nebular No StringBuilder is a class in the java.lang package that is preferable to use when creating a string bit by bit, over many steps: docs.oracle.com/javase/7/docs/api/java/lang/StringBuilder.html. The append(char c) method simply adds the char c to the end of the string
So it is called to manipulate the characters within the string? not sure what you mean by c.
|
0
public String getPassword(int no) {
        String inputChars= "ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";
        StringBuilder salt = new StringBuilder();
        Random rnd = new Random();
        while (salt.length() < no) {
            int index = (int) (rnd.nextFloat() * inputChars.length());
            salt.append(inputChars.charAt(index));
        }
        String password= salt.toString();
        return password;

    }

But I will recommend to use java.security.SecureRandom; e.g.

private SecureRandom random = new SecureRandom();

public String nextSessionId() {
    return new BigInteger(130, random).toString(32);
}

more secure way to genarate password

Comments

0

You can use Collections.shuffle() to shuffle a list. You can use the same to shuffle the chars in a string. Have a look into the below code. This code will return a unique randomized string of a given string on each execution.

    List<String> list = new ArrayList<>(Arrays.asList("abcdefghijklmnopqrstuvwxyz".split("")));
    Collections.shuffle(list);
    String randomString = list.stream().collect(Collectors.joining());  // Java 8

If all you need is some random chars, that too for purposes of security, you should use some sort of hashing. May be you can use UUID.randomUUID() for getting a random value, then you can hash the same to get always unique strings.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.