List of usage examples for java.util Random nextLong
public long nextLong()
From source file:com.android.volley.utils.CacheTestUtils.java
/** * Makes a random cache entry./*w ww . j av a 2 s. c om*/ * @param data Data to use, or null to use random data * @param isExpired Whether the TTLs should be set such that this entry is expired * @param needsRefresh Whether the TTLs should be set such that this entry needs refresh */ public static Cache.Entry makeRandomCacheEntry(byte[] data, boolean isExpired, boolean needsRefresh) { Random random = new Random(); Cache.Entry entry = new Cache.Entry(); if (data != null) { entry.data = data; } else { entry.data = new byte[random.nextInt(1024)]; } entry.etag = String.valueOf(random.nextLong()); entry.lastModified = random.nextLong(); entry.ttl = isExpired ? 0 : Long.MAX_VALUE; entry.softTtl = needsRefresh ? 0 : Long.MAX_VALUE; return entry; }
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);// w ww.j ava 2s .c o m LOG.info("spec: " + s); final DataInputBuffer in = new DataInputBuffer(); final DataOutputBuffer out = new DataOutputBuffer(); a.setType(GridmixKey.REDUCE_SPEC); 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:com.linkedin.pinot.core.data.readers.PinotSegmentUtil.java
private static Object generateSingleValue(Random random, FieldSpec.DataType dataType) { switch (dataType) { case INT:// w w w.j ava2 s . com return Math.abs(random.nextInt()); case LONG: return Math.abs(random.nextLong()); case FLOAT: return Math.abs(random.nextFloat()); case DOUBLE: return Math.abs(random.nextDouble()); case STRING: return RandomStringUtils.randomAlphabetic(DEFAULT_STRING_VALUE_LENGTH); default: throw new IllegalStateException("Illegal data type"); } }
From source file:org.terracotta.statistics.derived.histogram.HistogramFittingTest.java
@Parameterized.Parameters(name = "{index}: seed={0}, bias={1}, bars={2}") public static Iterable<Object[]> data() { Random rndm = new Random(); // seed, bias, bars, slope-error, centroid-error, width-error return Arrays.asList(new Object[][] { { rndm.nextLong(), 0.01, 20, 0.00205, 0.0153, 0.0121 }, { rndm.nextLong(), 0.01, 100, 0.00202, 0.00551, 0.00479 }, { rndm.nextLong(), 0.01, 1000, 0.00248, 0.00509, 0.00497 }, { rndm.nextLong(), 0.1, 20, 0.00137, 0.00710, 0.00746 }, { rndm.nextLong(), 0.1, 100, 0.00130, 0.00511, 0.00510 }, { rndm.nextLong(), 0.1, 1000, 0.00134, 0.00480, 0.00509 }, { rndm.nextLong(), 1, 20, 0.00118, 0.00557, 0.00703 }, { rndm.nextLong(), 1, 100, 0.00110, 0.00508, 0.00532 }, { rndm.nextLong(), 1, 1000, 0.00102, 0.00497, 0.00531 }, { rndm.nextLong(), 10, 20, 0.00134, 0.00712, 0.00797 }, { rndm.nextLong(), 10, 100, 0.00124, 0.00499, 0.00500 }, { rndm.nextLong(), 10, 1000, 0.00131, 0.00499, 0.00512 }, { rndm.nextLong(), 100, 20, 0.00193, 0.0151, 0.0123 }, { rndm.nextLong(), 100, 100, 0.00199, 0.00527, 0.00471 }, { rndm.nextLong(), 100, 1000, 0.00253, 0.00506, 0.00473 }, }); }
From source file:com.yenlo.synapse.transport.vfs.VFSUtils.java
/** * Acquires a file item lock before processing the item, guaranteing that the file is not * processed while it is being uploaded and/or the item is not processed by two listeners * * @param fsManager used to resolve the processing file * @param fo representing the processing file item * @return boolean true if the lock has been acquired or false if not *///from ww w . j av a 2s .c om public synchronized static boolean acquireLock(FileSystemManager fsManager, FileObject fo) { // generate a random lock value to ensure that there are no two parties // processing the same file Random random = new Random(); byte[] lockValue = String.valueOf(random.nextLong()).getBytes(); try { // check whether there is an existing lock for this item, if so it is assumed // to be processed by an another listener (downloading) or a sender (uploading) // lock file is derived by attaching the ".lock" second extension to the file name String fullPath = fo.getName().getURI(); int pos = fullPath.indexOf("?"); if (pos != -1) { fullPath = fullPath.substring(0, pos); } FileObject lockObject = fsManager.resolveFile(fullPath + ".lock"); if (lockObject.exists()) { log.debug("There seems to be an external lock, aborting the processing of the file " + fo.getName() + ". This could possibly be due to some other party already " + "processing this file or the file is still being uploaded"); } else { // write a lock file before starting of the processing, to ensure that the // item is not processed by any other parties lockObject.createFile(); OutputStream stream = lockObject.getContent().getOutputStream(); try { stream.write(lockValue); stream.flush(); stream.close(); } catch (IOException e) { lockObject.delete(); log.error("Couldn't create the lock file before processing the file " + fullPath, e); return false; } finally { lockObject.close(); } // check whether the lock is in place and is it me who holds the lock. This is // required because it is possible to write the lock file simultaneously by // two processing parties. It checks whether the lock file content is the same // as the written random lock value. // NOTE: this may not be optimal but is sub optimal FileObject verifyingLockObject = fsManager.resolveFile(fullPath + ".lock"); if (verifyingLockObject.exists() && verifyLock(lockValue, verifyingLockObject)) { return true; } } } catch (FileSystemException fse) { log.error("Cannot get the lock for the file : " + maskURLPassword(fo.getName().getURI()) + " before processing"); } return false; }
From source file:com.abiquo.appliancemanager.ApplianceManagerAsserts.java
protected static File createUploadTempFile() throws IOException { Random rnd = new Random(System.currentTimeMillis()); final String fileName = String.valueOf(rnd.nextLong()); File file = File.createTempFile(fileName, ".uploadTest"); RandomAccessFile f = new RandomAccessFile(file, "rw"); f.setLength(UPLOAD_FILE_SIZE_BYTES); file.deleteOnExit();//from w ww. jav a 2s . c om return file; }
From source file:org.apache.blur.shell.DiscoverFileBufferSizeUtil.java
private static long getPosition(int bufSize, Random random, long length) { return Math.abs(random.nextLong()) % (length - bufSize); }
From source file:org.finra.herd.core.AbstractCoreTest.java
/** * Returns a random date./*www. j av a 2 s. co m*/ */ public static Date getRandomDate() { Random rnd = new Random(); return new Date(Math.abs(System.currentTimeMillis() - rnd.nextLong())); }
From source file:org.finra.herd.core.AbstractCoreTest.java
/** * Returns a random date in future.// w ww . j a v a 2 s. co m */ public static Date getRandomDateInFuture() { Random rnd = new Random(); return new Date(Math.abs(System.currentTimeMillis() + rnd.nextLong())); }
From source file:org.eclipse.tracecompass.statesystem.core.tests.perf.historytree.HistoryTreeBackendBenchmark.java
/** * Get a next random value between 1 and a boundary. */// w ww. j a v a 2 s . c o m private static long getNextRandomValue(Random randomGenerator, long limit) { long nextLong = Math.abs(randomGenerator.nextLong()); long nextDelta = (nextLong % limit) + 1; return nextDelta; }