List of usage examples for java.util Random setSeed
public synchronized void setSeed(long seed)
From source file:Main.java
public static void main(String args[]) { Random randomno = new Random(); // setting seed randomno.setSeed(20); // value after setting seed System.out.println("Object after seed: " + randomno.nextInt()); }
From source file:org.apache.hadoop.hbase.regionserver.TestPerColumnFamilyFlush.java
public static void main(String[] args) throws Exception { int numRegions = Integer.parseInt(args[0]); long numRows = Long.parseLong(args[1]); HTableDescriptor htd = new HTableDescriptor(TABLENAME); htd.setMaxFileSize(10L * 1024 * 1024 * 1024); htd.setValue(HTableDescriptor.SPLIT_POLICY, ConstantSizeRegionSplitPolicy.class.getName()); htd.addFamily(new HColumnDescriptor(FAMILY1)); htd.addFamily(new HColumnDescriptor(FAMILY2)); htd.addFamily(new HColumnDescriptor(FAMILY3)); Configuration conf = HBaseConfiguration.create(); Connection conn = ConnectionFactory.createConnection(conf); Admin admin = conn.getAdmin();/* ww w . j a va 2 s. c om*/ if (admin.tableExists(TABLENAME)) { admin.disableTable(TABLENAME); admin.deleteTable(TABLENAME); } if (numRegions >= 3) { byte[] startKey = new byte[16]; byte[] endKey = new byte[16]; Arrays.fill(endKey, (byte) 0xFF); admin.createTable(htd, startKey, endKey, numRegions); } else { admin.createTable(htd); } admin.close(); Table table = conn.getTable(TABLENAME); byte[] qf = Bytes.toBytes("qf"); Random rand = new Random(); byte[] value1 = new byte[16]; byte[] value2 = new byte[256]; byte[] value3 = new byte[4096]; for (long i = 0; i < numRows; i++) { Put put = new Put(Hashing.md5().hashLong(i).asBytes()); rand.setSeed(i); rand.nextBytes(value1); rand.nextBytes(value2); rand.nextBytes(value3); put.addColumn(FAMILY1, qf, value1); put.addColumn(FAMILY2, qf, value2); put.addColumn(FAMILY3, qf, value3); table.put(put); if (i % 10000 == 0) { LOG.info(i + " rows put"); } } table.close(); conn.close(); }
From source file:com.openteach.diamond.network.waverider.network.Packet.java
public static void main(String[] args) { /*System.out.println("[DEBUG] Packet Header size = " + getHeaderSize()); //from w w w .j a v a 2 s . com SlaveState slaveState = new SlaveState(); slaveState.setId(1L); slaveState.setIsMasterCandidate(false); Command command = CommandFactory.createHeartbeatCommand(slaveState.toByteBuffer()); Packet packet = Packet.newDataPacket(command); ByteBuffer buffer = packet.marshall(); Packet p = Packet.unmarshall(buffer); Command cmd = Command.unmarshall(p.getPayLoad()); SlaveState ss = SlaveState.fromByteBuffer(cmd.getPayLoad()); System.out.println(cmd.toString()); // Test 2 MasterState masterState = new MasterState(); masterState.setId(1L); masterState.setIp("127.0.0.1"); masterState.setPort(8206); List<SessionState> sessionStateList = new LinkedList<SessionState>(); masterState.setSessionStateList(sessionStateList); SessionState sessionState = null; for(int i = 0; i < 10; i++) { sessionState = new SessionState(); sessionState.setIp("127.0.0.1"); sessionState.setPriority(1L); sessionState.setIsMasterCandidate(false); sessionStateList.add(sessionState); } Command command2 = CommandFactory.createHeartbeatCommand(masterState.toByteBuffer()); Packet packet2 = Packet.newDataPacket(command2); ByteBuffer buffer2 = packet2.marshall(); Packet p2 = Packet.unmarshall(buffer2); Command cmd2 = Command.unmarshall(p2.getPayLoad()); MasterState ms = MasterState.fromByteBuffer(cmd2.getPayLoad()); System.out.println(cmd.toString()); */ System.out.println("[DEBUG] Packet Header size = " + getHeaderSize()); BlockingQueue<ByteBuffer> queue = new LinkedBlockingQueue<ByteBuffer>(); Random rand = new Random(); int count = 0; int size = 0; for (int i = 0; i < 100000; i++) { SlaveState state = new SlaveState(); state.setId(Long.valueOf(i)); state.setIsMasterCandidate(true); Packet packet = Packet.newDataPacket( CommandFactory.createCommand(Command.AVAILABLE_COMMAND_START, state.toByteBuffer())); ByteBuffer buffer = packet.marshall(); rand.setSeed(System.currentTimeMillis()); count = rand.nextInt(100) + 1; size = buffer.remaining() / count; for (int j = 0; j < count; j++) { ByteBuffer buf = null; if (j == count - 1) { buf = ByteBuffer.allocate(buffer.remaining() - j * size); buf.put(buffer.array(), j * size, buffer.remaining() - j * size); } else { buf = ByteBuffer.allocate(size); buf.put(buffer.array(), j * size, size); } buf.flip(); queue.add(buf); } } for (int i = 0; i < 100000; i++) { //Packet packet = Packet.parse(queue); //Command commad = Command.unmarshall(packet.getPayLoad()); //SlaveState state = SlaveState.fromByteBuffer(commad.getPayLoad()); //System.out.println(state.toString()); } }
From source file:Main.java
public static long initRandomSerial() { final Random rnd = new Random(); rnd.setSeed(System.currentTimeMillis()); // prevent browser certificate caches, cause of doubled serial numbers // using 48bit random number long sl = ((long) rnd.nextInt()) << 32 | (rnd.nextInt() & 0xFFFFFFFFL); // let reserve of 16 bit for increasing, serials have to be positive sl = sl & 0x0000FFFFFFFFFFFFL;/* w w w . ja va 2s .co m*/ return sl; }
From source file:Main.java
public static String generatePassword() { Random r[] = new Random[8]; r[0] = new Random(); r[1] = new Random(); r[2] = new Random(); r[3] = new Random(); r[4] = new Random(); r[5] = new Random(); r[6] = new Random(); r[7] = new Random(); Random x = new Random(); StringBuilder password = new StringBuilder(); int length = 6; password.setLength(length);//w w w. j a v a 2s .c o m for (int i = 0; i < length; i++) { x.setSeed(r[i % 8].nextInt(500) * r[4].nextInt(900)); password.setCharAt(i, (char) (r[x.nextInt(256) % 8].nextInt(95) + 32)); } return password.toString(); }
From source file:net.xxs.util.EncodeUtils.java
/** * ????// w w w. ja va2 s . c om * * @param pwd_len * ?? * @return ? */ public static String genRandomNum(int pwd_len) { // 35026?+10 final int maxNum = 10; int i; // ?? int count = 0; // ?? /* * char[] str = { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', * 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', * 'z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' }; */ char[] str = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' }; StringBuffer pwd = new StringBuffer(""); Random r = new Random(); r.setSeed(System.currentTimeMillis()); while (count < pwd_len) { // ????? i = Math.abs(r.nextInt(maxNum)); // ?36-1 if (i >= 0 && i < str.length) { pwd.append(str[i]); count++; } } return pwd.toString(); }
From source file:org.apache.hadoop.mapred.gridmix.TestFilePool.java
@BeforeClass public static void setup() throws IOException { final Configuration conf = new Configuration(); final FileSystem fs = FileSystem.getLocal(conf).getRaw(); fs.delete(base, true);/*from w ww . j ava2s . c o m*/ final Random r = new Random(); final long seed = r.nextLong(); r.setSeed(seed); LOG.info("seed: " + seed); fs.mkdirs(base); for (int i = 0; i < NFILES; ++i) { Path file = base; for (double d = 0.6; d > 0.0; d *= 0.8) { if (r.nextDouble() < d) { file = new Path(base, Integer.toString(r.nextInt(3))); continue; } break; } OutputStream out = null; try { out = fs.create(new Path(file, "" + (char) ('A' + i))); final byte[] b = new byte[1024]; Arrays.fill(b, (byte) ('A' + i)); for (int len = ((i % 13) + 1) * 1024; len > 0; len -= 1024) { out.write(b); } } finally { if (out != null) { out.close(); } } } }
From source file:org.apache.hadoop.mapred.gridmix.TestGridmixRecord.java
static void eqSeedTest(GridmixRecord x, GridmixRecord y, int max) throws Exception { final Random r = new Random(); final long s = r.nextLong(); r.setSeed(s); LOG.info("eqSeed: " + s); assertEquals(x.fixedBytes(), y.fixedBytes()); final int min = x.fixedBytes() + 1; final DataOutputBuffer out1 = new DataOutputBuffer(); final DataOutputBuffer out2 = new DataOutputBuffer(); for (int i = min; i < max; ++i) { final long seed = r.nextLong(); setSerialize(x, seed, i, out1);/*from ww w . jav a2 s . co m*/ setSerialize(y, seed, i, out2); assertEquals(x, y); assertEquals(x.hashCode(), y.hashCode()); // verify written contents match assertEquals(out1.getLength(), out2.getLength()); // assumes that writes will grow buffer deterministically assertEquals("Bad test", out1.getData().length, out2.getData().length); assertArrayEquals(out1.getData(), out2.getData()); } }
From source file:org.apache.hadoop.mapred.gridmix.TestGridmixRecord.java
static void lengthTest(GridmixRecord x, GridmixRecord y, int min, int max) throws Exception { final Random r = new Random(); final long seed = r.nextLong(); r.setSeed(seed); LOG.info("length: " + seed); final DataInputBuffer in = new DataInputBuffer(); final DataOutputBuffer out1 = new DataOutputBuffer(); final DataOutputBuffer out2 = new DataOutputBuffer(); for (int i = min; i < max; ++i) { setSerialize(x, r.nextLong(), i, out1); // check write assertEquals(i, out1.getLength()); // write to stream x.write(out2);//from w w w.j a va 2 s .c om // check read in.reset(out1.getData(), 0, out1.getLength()); y.readFields(in); assertEquals(i, x.getSize()); assertEquals(i, y.getSize()); } // check stream read in.reset(out2.getData(), 0, out2.getLength()); for (int i = min; i < max; ++i) { y.readFields(in); assertEquals(i, y.getSize()); } }
From source file:org.apache.hadoop.mapred.gridmix.TestGridmixRecord.java
static void randomReplayTest(GridmixRecord x, GridmixRecord y, int min, int max) throws Exception { final Random r = new Random(); final long seed = r.nextLong(); r.setSeed(seed); LOG.info("randReplay: " + seed); final DataOutputBuffer out1 = new DataOutputBuffer(); for (int i = min; i < max; ++i) { final int s = out1.getLength(); x.setSeed(r.nextLong());// w w w . j a v a 2s . c o m x.setSize(i); x.write(out1); assertEquals(i, out1.getLength() - s); } final DataInputBuffer in = new DataInputBuffer(); in.reset(out1.getData(), 0, out1.getLength()); final DataOutputBuffer out2 = new DataOutputBuffer(); // deserialize written records, write to separate buffer for (int i = min; i < max; ++i) { final int s = in.getPosition(); y.readFields(in); assertEquals(i, in.getPosition() - s); y.write(out2); } // verify written contents match assertEquals(out1.getLength(), out2.getLength()); // assumes that writes will grow buffer deterministically assertEquals("Bad test", out1.getData().length, out2.getData().length); assertArrayEquals(out1.getData(), out2.getData()); }