Answers for "how to choose a random character from a string java"

1

create a random char java

import java.util.Random;

public class RandomChar {
    public static void main(String[] args) {
        Random random = new Random();

        char randomizedCharacter = (char) (random.nextInt(26) + 'a');
        System.out.println("Generated Random Character: " + randomizedCharacter);

    }

}
Posted by: Guest on April-17-2021
3

generate random string in java

import java.util.Random;

public class Generator {
	public static String generateRandomPassword(int len) {
		String chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijk"
          +"lmnopqrstuvwxyz!@#$%&";
		Random rnd = new Random();
		StringBuilder sb = new StringBuilder(len);
		for (int i = 0; i < len; i++)
			sb.append(chars.charAt(rnd.nextInt(chars.length())));
		return sb.toString();
	}
}
Posted by: Guest on July-16-2020

Code answers related to "how to choose a random character from a string java"

Code answers related to "Java"

Java Answers by Framework

Browse Popular Code Answers by Language