1

I'm trying to generate a string between capital A-Z in java using Secure Random. Currently I'm able to generate an alphanumeric string with special characters but I want a string with only upper case alphabets.

 public String createRandomCode(int codeLength, String id){ 
 char[] chars = id.toCharArray();
 StringBuilder sb = new StringBuilder();
 Random random = new SecureRandom();
 for (int i = 0; i < codeLength; i++) {
 char c = chars[random.nextInt(chars.length)];
 sb.append(c);
 }
 String output = sb.toString();
 System.out.println(output);
 return output ;
 } 

The input parameters are length of the output string & id whhich is alphanumeric string.Can't understand what modifications to make to the above code to generate only upper case alphabet string. Please help..

asked Aug 30, 2016 at 8:06
0

3 Answers 3

4

Your method randomly selects characters out of the id argument. If you want those to only be uppercase letters, then pass a string with those characters:

String randomCode = createRandomCode(length, "ABCDEFGHIJKLMNOPQRSTUVWXYZ");

EDIT If you want to avoid duplicates, you can't just select characters at random. You'll want to shuffle them and pick out the first n characters:

public String createRandomCode(int codeLength, String id) { 
 List<Character> temp = id.chars()
 .mapToObj(i -> (char)i)
 .collect(Collectors.toList());
 Collections.shuffle(temp, new SecureRandom());
 return temp.stream()
 .map(Object::toString)
 .limit(codeLength)
 .collect(Collectors.joining());
}

EDIT 2 Just for fun, here's another way to implement the original random code generator (allowing duplicates):

public static String createRandomCode(int codeLength, String id) {
 return new SecureRandom()
 .ints(codeLength, 0, id.length())
 .mapToObj(id::charAt)
 .map(Object::toString)
 .collect(Collectors.joining());
}
answered Aug 30, 2016 at 8:31
Sign up to request clarification or add additional context in comments.

1 Comment

What changes do I need to make in the above code so that the string contains no repeating alphabets??
3

Here is generator that I wrote and use:

public class RandomGenerator {
 private static final String characters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
 public static String generateRandom(int length) {
 Random random = new SecureRandom();
 if (length <= 0) {
 throw new IllegalArgumentException("String length must be a positive integer");
 }
 StringBuilder sb = new StringBuilder(length);
 for (int i = 0; i < length; i++) {
 sb.append(characters.charAt(random.nextInt(characters.length())));
 }
 return sb.toString();
 }
}

in numChars string you can put any characters you want to be included. int length parameter is the length of generated random string.

answered Aug 30, 2016 at 8:09

Comments

0

Here is an example method that uses the int range for characters A to Z (also this method avoids duplicate characters in the String) :

public String createRandomCode(final int codeLength) {
 int min = 65;// A
 int max = 90;// Z
 StringBuilder sb = new StringBuilder();
 Random random = new SecureRandom();
 for (int i = 0; i < codeLength; i++) {
 Character c;
 do {
 c = (char) (random.nextInt((max - min) + 1) + min);
 } while (sb.indexOf(c.toString()) > -1);
 sb.append(c);
 }
 String output = sb.toString();
 System.out.println(output);
 return output;
}

The range part comes from this topic : Generating random integers in a specific range

answered Aug 30, 2016 at 8:23

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.