Answers for "java create random string"

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
1

java random string

public static String getRandomString(int size) {
  	// The string that we will return
	String rand = "";
  	// The chars that are used to generate the random string
	String chars = "1234567890-=!@#$%^&*()_+qwertyuiop[]\\QWERTYUIOP{}|asdfghjkl;'ASDFGHJKL:\"zxcvbnm,./ZXCVBNM<>?";
  	// Loop based on the requested size
  	for (int i = 0; i < size; i++) {
      	// Add a random char from the chars string to the rand string
		rand += chars.toCharArray()[new Random().nextInt(chars.length())];
	}
  	// Return the random string
	return rand;
}
Posted by: Guest on May-28-2021
1

generate 5 digit random string in java

RandomStringUtils.randomAlphanumeric(17).toUpperCase()
Posted by: Guest on September-01-2020
-1

generate a random string in java

@Test
public void givenUsingJava8_whenGeneratingRandomAlphabeticString_thenCorrect() {
    int leftLimit = 97; // letter 'a'
    int rightLimit = 122; // letter 'z'
    int targetStringLength = 10;
    Random random = new Random();

    String generatedString = random.ints(leftLimit, rightLimit + 1)
      .limit(targetStringLength)
      .collect(StringBuilder::new, StringBuilder::appendCodePoint, StringBuilder::append)
      .toString();

    System.out.println(generatedString);
}
Posted by: Guest on July-31-2021

Code answers related to "Java"

Java Answers by Framework

Browse Popular Code Answers by Language