Java Random String getRandomString(int strLength)

Here you can find the source of getRandomString(int strLength)

Description

get Random String

License

Apache License

Declaration

public static String getRandomString(int strLength) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

import java.util.Random;

public class Main {

    public static String getRandomString(int strLength) {
        StringBuffer buffer = new StringBuffer();
        Random random = new Random();

        for (int i = 0; i < strLength; ++i) {
            int charInt;
            char c;
            if (random.nextBoolean()) {
                charInt = 48 + random.nextInt(10);
                c = (char) charInt;
                buffer.append(c);//from  w  w  w. j  a v a2 s  .  c  om
            } else {
                if (random.nextBoolean()) {
                    charInt = 65 + random.nextInt(26);
                } else {
                    charInt = 97 + random.nextInt(26);
                }

                if (charInt == 79) {
                    charInt = 111;
                }

                c = (char) charInt;
                buffer.append(c);
            }
        }

        return buffer.toString();
    }
}

Related

  1. getRandomString(int size)
  2. getRandomString(int size)
  3. getRandomString(int size)
  4. getRandomString(int size)
  5. getRandomString(int size)
  6. getRandomString(Random random, int minLen, int maxLen)
  7. getRandomString(Random random, int totalLength, int randomSectionLength)
  8. getRandomString(Random rnd, int minLength, int maxLength)
  9. getRandomString(String paramString, int paramLength)