Example usage for java.util Random Random

List of usage examples for java.util Random Random

Introduction

In this page you can find the example usage for java.util Random Random.

Prototype

public Random() 

Source Link

Document

Creates a new random number generator.

Usage

From source file:Main.java

private static int getRandomNum(int n) {
    new Color();
    return new Random().nextInt(n);
}

From source file:Main.java

public static void shuffleArray(PointF[] ar, int length) {
    // If running on Java 6 or older, use `new Random()` on RHS
    Random rnd;//from  ww w .j a va 2s  .  c  o m
    if (Build.VERSION.SDK_INT >= 21)
        rnd = ThreadLocalRandom.current();
    else
        rnd = new Random();

    for (int i = length - 1; i > 0; i--) {
        int index = rnd.nextInt(i + 1);
        // Simple swap
        PointF a = ar[index];
        ar[index] = ar[i];
        ar[i] = a;
    }
}

From source file:Main.java

/**
 * This method generates a random n digit password, which contains at least one number, lower case alphabet, upper
 * case alphabet and as special character.
 *///from   w  w  w. j a va2s .  com
public static String generatePassword(int n) {

    Random rd = new Random();

    char lowerChars[] = "abcdefghijklmnopqrstuvwxyz".toCharArray();
    char upperChars[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ".toCharArray();
    char numbers[] = "0123456789".toCharArray();
    char specialChars[] = "~!@#$%^&*()-_=+[{]}|;:<>/?".toCharArray();

    List<Character> pwdLst = new ArrayList<Character>();
    for (int g = 0; g < 4; g++) {
        for (int z = 0; z < 1; z++) {
            if (g == 0) {
                pwdLst.add(numbers[rd.nextInt(10)]);
            } else if (g == 1) {
                pwdLst.add(lowerChars[rd.nextInt(26)]);
            } else if (g == 2) {
                pwdLst.add(upperChars[rd.nextInt(26)]);
            } else if (g == 3) {
                pwdLst.add(specialChars[rd.nextInt(26)]);
            }
        }
        if (pwdLst.size() == n) {
            break;
        }
        if (g + 1 == 4) {
            g = (int) Math.random() * 5;

        }
    }
    StringBuilder password = new StringBuilder();
    Collections.shuffle(pwdLst);
    for (int c = 0; c < pwdLst.size(); c++) {
        password.append(pwdLst.get(c));
    }
    return password.toString();
}

From source file:Main.java

public static byte[] getDHdataWithHash(byte[] hash, byte[] data) {
    int ost = (data.length + 20) % 16 > 0 ? 16 : 0;
    int size = ((20 + data.length) / 16) * 16 + ost;

    ByteBuffer buffer = ByteBuffer.allocate(size);
    buffer.put(hash);//from  w w w .ja  v a2 s .  com
    buffer.put(data);

    byte[] rand = new byte[size - hash.length - data.length];
    new Random().nextBytes(rand);
    buffer.put(rand);
    return buffer.array();
}

From source file:com.pamarin.income.util.StringRandom.java

public static String random2048bit() {
    byte[] r = new byte[256]; //Means 2048 bit
    Random random = new Random();
    random.nextBytes(r);/*from  ww w.  ja  v a 2s. co  m*/

    return Base64.encodeBase64String(r).replace("+", "_");
}

From source file:Main.java

public static String getRandomChar() {
    String[] arr = { "2", "3", "4", "5", "6", "7", "8", "9", "a", "b", "c", "d", "e", "f", "g", "h", "i", "j",
            "k", "m", "n", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", "A", "B", "C", "D", "E", "F",
            "G", "H", "I", "J", "K", "L", "M", "N", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z" };

    Random random = new Random();
    return arr[random.nextInt(57)];
}

From source file:Main.java

public static void saveBitmapToSD(Bitmap bitmap) {
    String root = Environment.getExternalStorageDirectory().toString();
    File myDir = new File(root + "/Download");
    myDir.mkdirs();/*from w w w  . j  a  va 2 s .c om*/
    Random generator = new Random();
    String fname = "Image-" + System.currentTimeMillis() + ".jpg";
    File file = new File(myDir, fname);
    if (file.exists())
        file.delete();
    try {
        FileOutputStream out = new FileOutputStream(file);
        bitmap.compress(Bitmap.CompressFormat.JPEG, 90, out);
        out.flush();
        out.close();

    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:Main.java

public static String generateString(int length) {

    StringBuilder tmp = new StringBuilder();
    for (char ch = '0'; ch <= '9'; ++ch)
        tmp.append(ch);/* ww w  . j  a v  a 2  s  .  co m*/
    for (char ch = 'a'; ch <= 'z'; ++ch)
        tmp.append(ch);
    char[] symbols = tmp.toString().toCharArray();
    char[] buf = new char[length];
    Random random = new Random();

    for (int idx = 0; idx < buf.length; ++idx)
        buf[idx] = symbols[random.nextInt(symbols.length)];
    return new String(buf);
}

From source file:Main.java

/**
 * Get a random double number within a given range;
 *//*from w ww .  java  2  s.  c  o m*/
public static double getRandomDouble(double min, double max) throws Exception {
    double range = max - min;

    double ret;

    Random r = new Random();
    ret = r.nextDouble() * range;

    return ret;
}

From source file:Main.java

public static void saveImage(Bitmap bmp) {
    String root = Environment.getExternalStorageDirectory().toString();
    File myDir = new File(root + "/abcxyz");
    myDir.mkdirs();/*ww  w.ja v a  2 s  .  com*/
    Random generator = new Random();
    int n = 10000;
    n = generator.nextInt(n);
    String name = "Image-" + n + ".jpg";
    File file = new File(myDir, name);
    if (file.exists())
        file.delete();

    try {
        FileOutputStream out = new FileOutputStream(file);
        bmp.compress(Bitmap.CompressFormat.JPEG, 100, out);
        out.flush();
        out.close();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
}