0

So, I found this function on the Internet and I would like to get some guidance on making strings use uppercase letters alongside lowercase ones, the code:

import java.util.Random;

public class randomstring {
    public void rand()
    {
        int leftLimit = 97; // letter 'a'
        int rightLimit = 122; // letter 'z'
        int targetStringLength = 5;
        Random random = new Random();
        StringBuilder buffer = new StringBuilder(targetStringLength);
        for (int i = 0; i < targetStringLength; i++) {
            int randomLimitedInt = leftLimit + (int) 
              (random.nextFloat() * (rightLimit - leftLimit + 1));
            buffer.append((char) randomLimitedInt);
        }
        String generatedString = buffer.toString();

        System.out.println(generatedString);
        
        
    }
    public static void main(String[] args) {
        randomstring random = new randomstring();
        random.rand();
    }
}
Vlad
  • 25
  • 6
  • Your code is most likely from this page[www.baeldung.com/java-random-string](https://www.baeldung.com/java-random-string#plainjava-bounded). Look further down the page to see how to create uppercase letters as well. – Eritrean Apr 09 '22 at 10:58
  • Does this answer your question? [How to generate a random alpha-numeric string](https://stackoverflow.com/questions/41107/how-to-generate-a-random-alpha-numeric-string) – Joe Apr 09 '22 at 11:22

4 Answers4

0

The issue with using ascii indexes, is that it's hard to use separate ranges, lower is [97;122] while upper is [65;90], better use indexing in a given list of chars

static final String upper = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
static final String lower = upper.toLowerCase();
static final String symbols = upper + lower;

public static void rand() {
    int rightLimit = symbols.length();
    int targetStringLength = 50;
    Random random = new Random();
    StringBuilder buffer = new StringBuilder(targetStringLength);

    for (int i = 0; i < targetStringLength; i++) {
        int randomLimitedInt = random.nextInt(rightLimit);
        buffer.append(symbols.charAt(randomLimitedInt));
    }

    String generatedString = buffer.toString();
    System.out.println(generatedString);
}
azro
  • 53,056
  • 7
  • 34
  • 70
0

Slightly different approach, use random nextBoolean() to check whether the generated char should be uppercase.

import java.util.Random;

public class randomstring {
    public void rand() {
        int leftLimit = 97; // letter 'a'
        int targetStringLength = 5;
        Random random = new Random();
        StringBuilder buffer = new StringBuilder(targetStringLength);
        for (int i = 0; i < targetStringLength; i++) {
            char c =(char) (leftLimit + random.nextInt(26));
            boolean shouldUseUppercase = random.nextBoolean();
            char character = shouldUseUppercase ? Character.toUpperCase(c) : c;
            buffer.append(character);
        }
        String generatedString = buffer.toString();
        System.out.println(generatedString);
    }

    public static void main(String[] args) {
        randomstring random = new randomstring();
        random.rand();
    }
}
naimdjon
  • 3,162
  • 1
  • 20
  • 41
0
import java.util.Random;

public class RandomString {

    public static void main(String[] args) {
        
        StringBuilder randomString = new StringBuilder();
                
        new Random().ints(65, 122)
                    .filter(i->!(i>90&&i<97))
                    .boxed()
                    .limit(12)
                    .map(c->Character.toChars(c))
                    .forEach(c->randomString.append(c));
                
        System.out.println(randomString);
        
    }
    
}

change the value in limit to adjust the size of random String

Manju DS
  • 37
  • 3
0

Easiest way is by using apache commons library:

RandomStringUtils.randomAlphabetic(10);
Vivek Goel
  • 762
  • 2
  • 10
  • 21