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(long seed) 

Source Link

Document

Creates a new random number generator using a single long seed.

Usage

From source file:Clases.Encriptar.java

public static String getCadenaAleatoria(int longitud) {//microtime
    String cadenaAleatoria = "";
    long milis = new java.util.GregorianCalendar().getTimeInMillis();
    Random r = new Random(milis);
    int i = 0;//  w w  w . j  ava2  s  .  co m
    while (i < longitud) {
        char c = (char) r.nextInt(255);
        if ((c >= '0' && c <= '9') || (c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') || (c == '$')
                || (c == '_')) {
            cadenaAleatoria += c;
            i++;
        }
    }
    return cadenaAleatoria;
}

From source file:Main.java

/**
 * Generates a random byte and returns it as a hexadecimal string.
 *
 * @return A random byte as hexadecimal string.
 *//* www . j  ava  2  s  .c  o  m*/
public static String generatedRandomByteAsHexString() {
    if (mRandom == null) {
        mRandom = new Random(new Date().getTime());
    }

    int randomInt8 = mRandom.nextInt(256);
    return Integer.toHexString(randomInt8);
}

From source file:net.kazyx.wirespider.TestUtil.java

public static byte[] fixedLengthRandomByteArray(int length) {
    System.out.println("Create random byte array: " + length);
    byte[] ba = new byte[length];
    Random rnd = new Random(20L);
    rnd.nextBytes(ba);/*from   w w  w  .j av a 2 s  .co  m*/
    return ba;
}

From source file:Main.java

public static String generateNickname() {
    Random rnd = new Random(System.currentTimeMillis());
    int race_selection = rnd.nextInt(3);

    String thefirstname = "";
    String thelastname = "";
    String thefirstname1 = "";
    String thelastname1 = "";

    if (race_selection == 0) {
        double fnprefix1 = Math.floor(Math.random() * 122);
        double fnsuffix1 = Math.floor(Math.random() * 91);
        double lnprefix1 = Math.floor(Math.random() * 67);
        double lnsuffix1 = Math.floor(Math.random() * 64);

        thefirstname = h_fnpre.get((int) fnprefix1) + h_fnsuf.get((int) fnsuffix1);
        thelastname = h_lnpre.get((int) lnprefix1) + h_lnsuf.get((int) lnsuffix1);

        thefirstname1 = thefirstname.substring(0, 1).toUpperCase();
        thefirstname = thefirstname1 + thefirstname.substring(1, thefirstname.length());

        thelastname1 = thelastname.substring(0, 1).toUpperCase();
        thelastname = thelastname1 + thelastname.substring(1, thelastname.length());

    } else if (race_selection == 0) {
        double fnprefix1 = Math.floor(Math.random() * 80);
        double fnsuffix1 = Math.floor(Math.random() * 67);

        thefirstname = o_fnpre.get((int) fnprefix1) + o_fnsuf.get((int) fnsuffix1);
        thelastname = "";

    } else {/*from w  w  w  .  j  a  v  a 2  s .c  o m*/
        double fnprefix1 = Math.floor(Math.random() * 122);
        double fnsuffix1 = Math.floor(Math.random() * 91);

        thefirstname = h_fnpre.get((int) fnprefix1) + h_fnsuf.get((int) fnsuffix1);

    }

    return thefirstname + " " + thelastname;
}

From source file:MainClass.java

private static void fillArray(Object array) {
    int length = Array.getLength(array);
    Random generator = new Random(System.currentTimeMillis());
    for (int i = 0; i < length; i++) {
        int random = generator.nextInt();
        Array.setInt(array, i, random);
    }//  w  w  w  . j a  v a  2  s. c o  m
}

From source file:Main.java

public static void genRandom(long seed, int factor, int offset, float array[]) {
    Random r = new Random(seed);
    for (int i = 0; i < array.length; i++) {
        array[i] = r.nextFloat() * factor + offset;
    }// ww  w . j  a va 2 s.  co m
}

From source file:Main.java

public static int randomInt(int min, int max) {
    Random random = new Random(System.currentTimeMillis());
    return min + random.nextInt(max - min);
}

From source file:com.fengduo.bee.commons.core.utils.Identities.java

/**
 * ??n?/*from  w  ww  .j a v  a2  s  .  com*/
 */
public static String randomNum(int n) {
    Random r = new Random(System.currentTimeMillis());
    StringBuffer buf = new StringBuffer(n);
    for (int i = 0; i < n; i++) {
        buf.append(r.nextInt(10));
    }
    return buf.toString();
}

From source file:Main.java

/**
 * Fills the array with random ints.  Values will be between min (inclusive) and
 * max (inclusive)./*ww  w.j a  v a2 s.co m*/
 */
public static void genRandomInts(long seed, int min, int max, int array[]) {
    Random r = new Random(seed);
    for (int i = 0; i < array.length; i++) {
        long range = max - min + 1;
        array[i] = (int) (min + r.nextLong() % range);
    }
    array[r.nextInt(array.length)] = min;
    array[r.nextInt(array.length)] = max;
}

From source file:org.jfree.chart.demo.HistogramDemo1.java

private static IntervalXYDataset createDataset() {
    HistogramDataset histogramdataset = new HistogramDataset();
    double ad[] = new double[1000];
    Random random = new Random(0xbc614eL);
    for (int i = 0; i < 1000; i++)
        ad[i] = random.nextGaussian() + 5D;

    histogramdataset.addSeries("H1", ad, 100, 2D, 8D);
    ad = new double[1000];
    for (int j = 0; j < 1000; j++)
        ad[j] = random.nextGaussian() + 7D;

    histogramdataset.addSeries("H2", ad, 100, 4D, 10D);
    return histogramdataset;
}