Java Random String getRandomString(int size)

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

Description

get Random String

License

Open Source License

Declaration

public static String getRandomString(int size) 

Method Source Code

//package com.java2s;

import java.util.Random;

public class Main {
    private static final Random RANDOM = new Random();
    private static final char[] CHARS = { '1', '2', '3', '4', '5', '6', '7', '8', '9', '0', 'q', 'w', 'e', 'r', 't',
            'y', 'u', 'i', 'o', 'p', 'a', 's', 'd', 'f', 'g', 'h', 'j', 'k', 'l', 'z', 'x', 'c', 'v', 'b', 'n', 'm',
            'Q', 'W', 'E', 'R', 'T', 'Y', 'U', 'I', 'O', 'P', 'A', 'S', 'D', 'F', 'G', 'H', 'J', 'K', 'L', 'Z', 'X',
            'C', 'V', 'B', 'N', 'M' };

    public static String getRandomString(int size) {
        StringBuilder s = new StringBuilder(size);
        int len = CHARS.length;
        for (int i = 0; i < size; i++) {
            int x = RANDOM.nextInt();
            s.append(CHARS[(x < 0 ? -x : x) % len]);
        }//from   ww w.  j  a v a  2  s .c o m
        return s.toString();
    }

    public static String toString(byte[] bytes) {
        if (bytes == null || bytes.length == 0) {
            return "";
        }
        StringBuffer buffer = new StringBuffer();
        for (byte byt : bytes) {
            buffer.append((char) byt);
        }
        return buffer.toString();
    }
}

Related

  1. getRandomString(int randomPasswordLength)
  2. getRandomString(int size)
  3. getRandomString(int size)
  4. getRandomString(int size)
  5. getRandomString(int size)
  6. getRandomString(int size)
  7. getRandomString(int strLength)
  8. getRandomString(Random random, int minLen, int maxLen)
  9. getRandomString(Random random, int totalLength, int randomSectionLength)