Example usage for java.util Random setSeed

List of usage examples for java.util Random setSeed

Introduction

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

Prototype

public synchronized void setSeed(long seed) 

Source Link

Document

Sets the seed of this random number generator using a single long seed.

Usage

From source file:org.apache.hadoop.mapred.gridmix.TestGridmixRecord.java

static void binSortTest(GridmixRecord x, GridmixRecord y, int min, int max, WritableComparator cmp)
        throws Exception {
    final Random r = new Random();
    final long s = r.nextLong();
    r.setSeed(s);
    LOG.info("sort: " + s);
    final DataOutputBuffer out1 = new DataOutputBuffer();
    final DataOutputBuffer out2 = new DataOutputBuffer();
    for (int i = min; i < max; ++i) {
        final long seed1 = r.nextLong();
        setSerialize(x, seed1, i, out1);
        assertEquals(0, x.compareSeed(seed1, Math.max(0, i - x.fixedBytes())));

        final long seed2 = r.nextLong();
        setSerialize(y, seed2, i, out2);
        assertEquals(0, y.compareSeed(seed2, Math.max(0, i - x.fixedBytes())));

        // for eq sized records, ensure byte cmp where req
        final int chk = WritableComparator.compareBytes(out1.getData(), 0, out1.getLength(), out2.getData(), 0,
                out2.getLength());/* www  .  jav a  2 s  . c om*/
        assertEquals(chk, x.compareTo(y));
        assertEquals(chk,
                cmp.compare(out1.getData(), 0, out1.getLength(), out2.getData(), 0, out2.getLength()));
        // write second copy, compare eq
        final int s1 = out1.getLength();
        x.write(out1);
        assertEquals(0, cmp.compare(out1.getData(), 0, s1, out1.getData(), s1, out1.getLength() - s1));
        final int s2 = out2.getLength();
        y.write(out2);
        assertEquals(0, cmp.compare(out2.getData(), 0, s2, out2.getData(), s2, out2.getLength() - s2));
        assertEquals(chk, cmp.compare(out1.getData(), 0, s1, out2.getData(), s2, out2.getLength() - s2));
    }
}

From source file:org.apache.hadoop.mapred.gridmix.TestGridmixRecord.java

static void checkSpec(GridmixKey a, GridmixKey b) throws Exception {
    final Random r = new Random();
    final long s = r.nextLong();
    r.setSeed(s);
    LOG.info("spec: " + s);
    final DataInputBuffer in = new DataInputBuffer();
    final DataOutputBuffer out = new DataOutputBuffer();
    a.setType(GridmixKey.REDUCE_SPEC);// www.jav  a2 s.c o  m
    b.setType(GridmixKey.REDUCE_SPEC);
    for (int i = 0; i < 100; ++i) {
        final int in_rec = r.nextInt(Integer.MAX_VALUE);
        a.setReduceInputRecords(in_rec);
        final int out_rec = r.nextInt(Integer.MAX_VALUE);
        a.setReduceOutputRecords(out_rec);
        final int out_bytes = r.nextInt(Integer.MAX_VALUE);
        a.setReduceOutputBytes(out_bytes);
        final int min = WritableUtils.getVIntSize(in_rec) + WritableUtils.getVIntSize(out_rec)
                + WritableUtils.getVIntSize(out_bytes);
        assertEquals(min + 2, a.fixedBytes()); // meta + vint min
        final int size = r.nextInt(1024) + a.fixedBytes() + 1;
        setSerialize(a, r.nextLong(), size, out);
        assertEquals(size, out.getLength());
        assertTrue(a.equals(a));
        assertEquals(0, a.compareTo(a));

        in.reset(out.getData(), 0, out.getLength());

        b.readFields(in);
        assertEquals(size, b.getSize());
        assertEquals(in_rec, b.getReduceInputRecords());
        assertEquals(out_rec, b.getReduceOutputRecords());
        assertEquals(out_bytes, b.getReduceOutputBytes());
        assertTrue(a.equals(b));
        assertEquals(0, a.compareTo(b));
        assertEquals(a.hashCode(), b.hashCode());
    }
}

From source file:org.apache.kylin.cube.inmemcubing.InMemCubeBuilderTest.java

static void feedData(final CubeInstance cube, final String flatTable, ArrayBlockingQueue<List<String>> queue,
        int count, long randSeed) throws IOException, InterruptedException {
    CubeJoinedFlatTableDesc flatTableDesc = new CubeJoinedFlatTableDesc(cube.getDescriptor(), null);
    int nColumns = flatTableDesc.getColumnList().size();

    @SuppressWarnings("unchecked")
    Set<String>[] distinctSets = new Set[nColumns];
    for (int i = 0; i < nColumns; i++)
        distinctSets[i] = new TreeSet<String>();

    // get distinct values on each column
    List<String> lines = FileUtils.readLines(new File(flatTable), "UTF-8");
    for (String line : lines) {
        String[] row = line.trim().split(",");
        assert row.length == nColumns;
        for (int i = 0; i < nColumns; i++)
            distinctSets[i].add(row[i]);
    }//from   w w  w  .  j a v a 2  s.  c o  m

    List<String[]> distincts = new ArrayList<String[]>();
    for (int i = 0; i < nColumns; i++) {
        distincts.add((String[]) distinctSets[i].toArray(new String[distinctSets[i].size()]));
    }

    Random rand = new Random();
    if (randSeed != 0)
        rand.setSeed(randSeed);

    // output with random data
    for (; count > 0; count--) {
        ArrayList<String> row = new ArrayList<String>(nColumns);
        for (int i = 0; i < nColumns; i++) {
            String[] candidates = distincts.get(i);
            row.add(candidates[rand.nextInt(candidates.length)]);
        }
        queue.put(row);
    }
    queue.put(new ArrayList<String>(0));
}

From source file:org.apache.kylin.cube.inmemcubing.ITInMemCubeBuilderTest.java

static void feedData(final CubeInstance cube, final String flatTable, ArrayBlockingQueue<List<String>> queue,
        int count, long randSeed) throws IOException, InterruptedException {
    IJoinedFlatTableDesc flatDesc = EngineFactory.getJoinedFlatTableDesc(cube.getDescriptor());
    int nColumns = flatDesc.getAllColumns().size();

    @SuppressWarnings("unchecked")
    Set<String>[] distinctSets = new Set[nColumns];
    for (int i = 0; i < nColumns; i++)
        distinctSets[i] = new TreeSet<String>();

    // get distinct values on each column
    List<String> lines = FileUtils.readLines(new File(flatTable), "UTF-8");
    for (String line : lines) {
        String[] row = line.trim().split(",");
        assert row.length == nColumns;
        for (int i = 0; i < nColumns; i++)
            distinctSets[i].add(row[i]);
    }//from  ww w  .  j  a  v a2 s . c o m

    List<String[]> distincts = new ArrayList<String[]>();
    for (int i = 0; i < nColumns; i++) {
        distincts.add((String[]) distinctSets[i].toArray(new String[distinctSets[i].size()]));
    }

    Random rand = new Random();
    if (randSeed != 0)
        rand.setSeed(randSeed);

    // output with random data
    for (; count > 0; count--) {
        ArrayList<String> row = new ArrayList<String>(nColumns);
        for (int i = 0; i < nColumns; i++) {
            String[] candidates = distincts.get(i);
            row.add(candidates[rand.nextInt(candidates.length)]);
        }
        queue.put(row);
    }
    queue.put(new ArrayList<String>(0));
}

From source file:com.sonicle.webtop.core.util.IdentifierUtils.java

/**
 * @deprecated use com.sonicle.commons.IdentifierUtils.getRandomAlphaNumericString instead
 * @return/*from  ww  w.j  ava  2 s.  c om*/
 */
@Deprecated
public static synchronized String getRandomAlphaNumericString(int length) {
    try {
        SecureRandom sr = SecureRandom.getInstance("SHA1PRNG");
        Random rand = new Random();
        char buff[] = new char[length];
        for (int i = 0; i < length; ++i) {
            // reseed rand once you've used up all available entropy bits
            if ((i % 10) == 0)
                rand.setSeed(sr.nextLong()); // 64 bits of random!
            buff[i] = VALID_CHARACTERS[rand.nextInt(VALID_CHARACTERS.length)];
        }
        return new String(buff);
    } catch (NoSuchAlgorithmException ex) {
        return null;
    }
}

From source file:org.apache.accumulo.server.test.TestIngest.java

public static byte[] genRandomValue(Random random, byte dest[], int seed, int row, int col) {
    random.setSeed((row ^ seed) ^ col);
    random.nextBytes(dest);/* w ww.j  a v  a2 s.  c om*/
    toPrintableChars(dest);

    return dest;
}

From source file:net.sf.keystore_explorer.crypto.privatekey.Pkcs8Util.java

private static int generateIterationCount() {
    // Generate a random iteration count in range 1000-1999
    Random rng = new Random();
    rng.setSeed(Calendar.getInstance().getTimeInMillis());

    int random = rng.nextInt();

    int mod1000 = random % 1000;

    return mod1000 + 1000;
}

From source file:net.sf.keystore_explorer.crypto.privatekey.Pkcs8Util.java

private static byte[] generateSalt() {
    // Generate random 8-bit salt
    Random random = new Random();
    random.setSeed(Calendar.getInstance().getTimeInMillis());

    byte[] salt = new byte[8];

    random.nextBytes(salt);//from w ww. ja  va 2 s  . c om

    return salt;
}

From source file:org.apache.mnemonic.Utils.java

/**
 * create a new instance of Random/*  ww  w.ja  v  a 2  s .  co m*/
 *
 * @param rgenseed
 *          specify a random seed
 *
 * @return the instance of Random
 */
public static Random createRandom(long rgenseed) {
    Random ret = new Random();
    if (0L == rgenseed) {
        rgenseed = System.currentTimeMillis();
        System.out.println("Random number generator seed is " + rgenseed);
    } else {
        System.out.println("Fixed Random number generator seed is " + rgenseed);
    }
    ret.setSeed(rgenseed);
    return ret;
}

From source file:com.github.nlloyd.hornofmongo.MongoScope.java

public static void _srand(Context cx, Scriptable thisObj, Object[] args, Function funObj) {
    Random randomGen = threadLocalRandomGen.get();
    if (args[0] instanceof Long)
        randomGen.setSeed((Long) args[0]);
    else/*  ww w  .  jav a 2 s  .co  m*/
        randomGen.setSeed(Double.valueOf(args[0].toString()).longValue());
}