Java Random String getRandomString()

Here you can find the source of getRandomString()

Description

Generates Random String of length 6 to 10.

License

Apache License

Declaration

public static String getRandomString() 

Method Source Code

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

public class Main {
    /**/*from  ww  w.  ja  v a2  s  . com*/
     * Generates Random String of length 6 to 10.
     * 
     * @return
     */
    public static String getRandomString() {
        return randomstring(6, 15);
    }

    public static String randomstring(int lo, int hi) {
        int n = rand(lo, hi);
        byte b[] = new byte[n];
        for (int i = 0; i < n; i++)
            b[i] = (byte) rand('a', 'z');
        return new String(b);
    }

    private static int rand(int lo, int hi) {
        java.util.Random rn = new java.util.Random();
        int n = hi - lo + 1;
        int i = rn.nextInt() % n;
        if (i < 0)
            i = -i;
        return lo + i;
    }
}

Related

  1. getRandomString()
  2. getRandomString()
  3. getRandomString()
  4. getRandomString()
  5. getRandomString()
  6. getRandomString(final int len)
  7. getRandomString(final int len)
  8. getRandomString(final int length)
  9. getRandomString(final int size)