Java Random String getRandomString()

Here you can find the source of getRandomString()

Description

get Random String

License

Apache License

Declaration

public static String getRandomString() 

Method Source Code


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

import java.util.Random;

public class Main {
    static Random random = new Random();

    public static String getRandomString() {
        return bytes2Hex(getRandomBytes());
    }/*from   w ww.j a  v a  2  s . c o m*/

    public static String bytes2Hex(byte[] b) {
        StringBuilder sb = new StringBuilder(1024);
        for (int n = 0; n < b.length; n++) {
            String s = Integer.toHexString(b[n] & 0xFF);
            sb.append((s.length() == 1) ? "0" + s : s);
        }
        return sb.toString();
    }

    public static byte[] getRandomBytes() {
        byte[] bytes = new byte[1024];
        random.nextBytes(bytes);
        return bytes;
    }
}

Related

  1. GetRandomStr(int length)
  2. getRandomStr(int length)
  3. getRandomStr(int n)
  4. getRandomStr(int qty)
  5. getRandomStr(String src)
  6. getRandomString()
  7. getRandomString()
  8. getRandomString()
  9. getRandomString()