Answers for "Random stringJava"

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

java 8 random string generator

@Test
public void givenUsingJava8_whenGeneratingRandomAlphanumericString_thenCorrect() {
    int leftLimit = 48; // numeral '0'
    int rightLimit = 122; // letter 'z'
    int targetStringLength = 10;
    Random random = new Random();
 
    String generatedString = random.ints(leftLimit, rightLimit + 1)
      .filter(i -> (i <= 57 || i >= 65) && (i <= 90 || i >= 97))
      .limit(targetStringLength)
      .collect(StringBuilder::new, StringBuilder::appendCodePoint, StringBuilder::append)
      .toString();
 
    System.out.println(generatedString);
}
Posted by: Guest on June-09-2020

Code answers related to "Java"

Java Answers by Framework

Browse Popular Code Answers by Language