collection of random strings, and every string with random characters (java)

java generate random alphanumeric string of specific length
how to generate random characters in java
how to choose a random character from a string java
java generate unique string fixed length
java random string from list
java random string from array
guava random string

I need to write a method that generates a random collection of strings, each of which is a string that contains from 6 to 15 characters long. The number of strings in the collection is passed as a parameter to the method. I think that strings should have different characters.

I tried, but there is a mistake in something. The collection gives the same count of strings and the same count of characters in every string. And the quantity of characters in a string conforms to the number of characters. I change int len on 5 for example then I receive 5 strings in a collection and 5 characters in a string.

public static void main(String[] args) {

    System.out.println(generateRandomString(5, 5));

public static String generateRandomString(int len, int count) {
    String letters = new String("abcdefghijklmnopqrstuvwxyz");
    StringBuffer word = new StringBuffer();
    List<StringBuffer> randomWords = new LinkedList<StringBuffer>();

    int randomIndex = 0;
    for (int i = 0; i < len; i++) {
        randomIndex = new Random().nextInt(letters.length());
        for (int j = 0; j < randomWords.size() && j <= count; j++);
    return randomWords.toString();

In console:

[kdvjw, kdvjw, kdvjw, kdvjw, kdvjw]

Here's a solution that returns a collection of random strings of different lengths that vary between given limits:

private static final String letters = "abcdefghijklmnopqrstuvwxyz";

public static String generateRandomString(int lower, int upper, int count) {
    final List<StringBuilder> randomWords = new ArrayList<>(count);
    final Random random = new Random();
    final int range = upper - lower + 1;

    while (--count >= 0) {
        int wordLength = lower + random.nextInt(range);
        final StringBuilder word = new StringBuilder(wordLength);

        while (--wordLength >= 0) {
    return randomWords.toString();

Is there functionality to generate a random character in Java?, Random r = new Random(); String alphabet = "123xyz"; for (int i = 0; i < 50; i++) in above 3 number for a , b , c or d and if u want all character like a to z then you You could create strings from a more restricted set of characters or with  The example also shows how to create random alphanumeric string, random numeric string or random alphabetic string in Java. How to create a random string of the specified length in Java? There are several ways in which you can create a random string in Java as given below. 1) Using the Random and String classes

At the moment you are adding the same reference to word to the List over and over again. This will make it so that you'll end up with a List of references to the same object.

Also you have a semicolon at the end of your for loop, and your loops are a little messed up. Right now you are adding word to every index of the List on every iteration of the outer loop. Instead, loop for as many Strings you want to return, and in the inner loop append onto the StringBuffer.

Also I would recommend making your Random object outside of the loop, so that a new one is not created on every iteration:

public static String generateRandomString(int len, int count) {
    String letters = "abcdefghijklmnopqrstuvwxyz";    
    Random rand = new Random();
    List<StringBuffer> randomWords = new LinkedList<StringBuffer>();
    int randomIndex = 0;
    for (int i = 0; i < count; i++) {

        StringBuffer word = new StringBuffer();
        for (int j = 0; j <= len; j++) {
            randomIndex = rand.nextInt(letters.length());
    return randomWords.toString();

Sample Output:

[oumcmb, vuovbx, sdysoo, aqxbxc, oijmmh]

Also just to note Apache Commons has a RandomStringUtils class which has a method to generate a random String of given length.

Java, Generate Bounded and Unbounded Random Strings using plain Let's start simple and generate a random String bounded to 7 characters: ? .collect(​StringBuilder:: new , StringBuilder::appendCodePoint, StringBuilder::append) So – instead of all the low-level code in the Java example – this one is  Description: Write a program to generate random string of length 10 charactors. Every time you call the method, the program should generate random string.

I think it is better to incapsulate this logic into separate class.

public final class RandomString {

    private static final char[] SYMBOLS = "abcdefghijklmnopqrstuvwxyz".toCharArray();

    private final Random random = new Random();

    public String nextString(int len) {
        StringBuilder buf = new StringBuilder(len);

        for (int i = 0; i < len; i++)

        return buf.toString();

An use it where you need:

public static Set<String> generateRandomString(int len, int count) {
    RandomString randomString = new RandomString();

    Set<String> res = new HashSet<>();

    for (int i = 0; i < count; i++)

    return res;

Generate random String of given size in Java, Java program generate a random AlphaNumeric String First take char between 0 to 256. remove all char except 0-9, a-z and A-Z. Random select an char; Add  Suppose I have a collection of 10 names. String[] names = new String[10]; // Make sure you init the array And on clicking a button the program should generate a random name from that collection. I assume by generate a random name from that collection you mean chose a random name from that collection.

How to create random string with random characters?, Every time you call the method, the program should generate random string. Code: ? 1. 2. 3. Few Java examples to show you how to generate a random alphanumeric String, with a fixed length. 1. Random [a-ZA-Z0-9] 1.1 Generate a random alphanumeric String [a-ZA-Z0-9], with a length of 8.

How to Create Random Alphabetic or AlphaNumeric String of given , How to generate Alphabetic String of given length in Java given length, we first create a source String by concatenating all possible characters as shown below: We can then use that random int to pick the character from the source string. Print the string after the specified character has occurred given no. of times in C Program Importance of deepToString() and asList() methods in Java? Selected Reading

Wicked Cool Java: Code Bits, Open-source Libraries, and Project Ideas, RandomStringUtils, that has a number of static utility methods for generating random text. The randomAlphanumeric method creates random alphanumeric text  1. Generate Random Unbounded String With Plain Java. Let's start simple and generate a random Stringbounded to 7 characters: @Testpublic void givenUsingPlainJava_whenGeneratingRandomStringUnbounded_thenCorrect() { byte[] array = new byte[7]; // length is bounded by 7 new Random().nextBytes(array); String generatedString = new String(array, Charset.forName("UTF-8")); System.out.println(generatedString);}

  • For any one call, are all the generated strings supposed to have the same length?
  • I think generated strings can have the different length and it will be more interestihg. But if it will be the same it will not be a mistake.
  • Thank you! Random strings of different lengths will be perfect. But it has a problem with adding the word to List<StringBuffer> randomWords, because the word has a StringBuilder type: word.append(letters.charAt(random.nextInt(letters.length()))); randomWords.add(word);
  • @YanaKit - Oops. The declaration of randomWords was wrong. I just updated it to be List<StringBuilder> instead of List<StringBuffer>.
  • Thanks once more! I've tryed it and now it works. But it gave me this result:
  • @YanaKit - What result?
  • For example: [nctph, nctph, nctph, nctph, nctph, wchk, wchk, wchk, wchk] or [dms, dms, dms, qonglg, qonglg, qonglg, qonglg, qonglg, qonglg]. Maybe it's right. I thought every word will be with random chars or not?