本文整理了Java中java.util.Random
类的一些代码示例,展示了Random
类的具体用法。这些代码示例主要来源于Github
/Stackoverflow
/Maven
等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Random
类的具体详情如下:
包路径:java.util.Random
类名称:Random
[英]This class provides methods that return pseudo-random values.
It is dangerous to seed Random with the current time because that value is more predictable to an attacker than the default seed.
This class is thread-safe.
[中]此类提供返回伪随机值的方法。
使用当前时间随机设定种子是危险的,因为攻击者比默认种子更容易预测该值。
这个类是线程安全的。
代码示例来源:origin: google/guava
static void assertHashByteBufferMatchesBytes(HashFunction hashFunction) {
Random rng = new Random(0L);
byte[] bytes = new byte[rng.nextInt(256) + 1];
rng.nextBytes(bytes);
assertEquals(hashFunction.hashBytes(bytes), hashFunction.hashBytes(ByteBuffer.wrap(bytes)));
}
代码示例来源:origin: skylot/jadx
public A test() {
Random random = new Random();
int a2 = random.nextInt();
int a3 = a2 + 3;
return new A(this, a2, a3, 4, 5, random.nextDouble()) {
@Override
public void m() {
System.out.println(1);
}
};
}
代码示例来源:origin: libgdx/libgdx
/** Creates a new random number generator. This constructor sets the seed of the random number generator to a value very likely
* to be distinct from any other invocation of this constructor.
* <p>
* This implementation creates a {@link Random} instance to generate the initial seed. */
public RandomXS128 () {
setSeed(new Random().nextLong());
}
代码示例来源:origin: commons-io/commons-io
public static byte[] generateRandomByteStream(final int pSize) {
final byte[] buffer = new byte[pSize];
final Random rnd = new Random();
rnd.nextBytes(buffer);
return buffer;
}
代码示例来源:origin: square/retrofit
/**
* Randomly determine whether this call should result in a network failure in accordance with
* configured behavior. When true, {@link #failureException()} should be thrown.
*/
public boolean calculateIsFailure() {
return random.nextInt(100) < failurePercent;
}
代码示例来源:origin: google/guava
public void testIteratorTester() throws Exception {
Random random = new Random(0);
List<Integer> list = Lists.newArrayList();
for (int i = 0; i < 3; i++) {
list.add(random.nextInt());
}
runIterator(list, 6);
}
代码示例来源:origin: google/guava
@Override
void performAction(Random random, Iterable<? extends PrimitiveSink> sinks) {
byte[] value = new byte[random.nextInt(128)];
random.nextBytes(value);
int off = random.nextInt(value.length + 1);
int len = random.nextInt(value.length - off + 1);
for (PrimitiveSink sink : sinks) {
sink.putBytes(value, off, len);
}
}
},
代码示例来源:origin: spring-projects/spring-framework
public AlternativeJdkIdGenerator() {
SecureRandom secureRandom = new SecureRandom();
byte[] seed = new byte[8];
secureRandom.nextBytes(seed);
this.random = new Random(new BigInteger(seed).longValue());
}
代码示例来源:origin: skylot/jadx
public void test() {
final int a = new Random().nextInt();
final long l = new Random().nextLong();
func(new A(l) {
@Override
public void m() {
System.out.println(a);
}
});
System.out.println("a" + a);
print(a);
print2(1, a);
print3(1, l);
}
代码示例来源:origin: google/guava
/**
* Generates a number in [0, 2^numBits) with an exponential distribution. The floor of the log2 of
* the absolute value of the result is chosen uniformly at random in [0, numBits), and then the
* result is chosen from those possibilities uniformly at random.
*
* <p>Zero is treated as having log2 == 0.
*/
static double randomDouble(int maxExponent) {
double result = RANDOM_SOURCE.nextDouble();
result = Math.scalb(result, RANDOM_SOURCE.nextInt(maxExponent + 1));
return RANDOM_SOURCE.nextBoolean() ? result : -result;
}
代码示例来源:origin: google/guava
public void testByteArrayRoundTrips() {
Random r = new Random(5);
byte[] b = new byte[Longs.BYTES];
for (int i = 0; i < 1000; i++) {
long num = r.nextLong();
assertEquals(num, Longs.fromByteArray(Longs.toByteArray(num)));
r.nextBytes(b);
long value = Longs.fromByteArray(b);
assertTrue("" + value, Arrays.equals(b, Longs.toByteArray(value)));
}
}
代码示例来源:origin: google/guava
private static ImmutableList<Double> generatePseudorandomDataset() {
Random random = new Random(2211275185798966364L);
ImmutableList.Builder<Double> largeDatasetBuilder = ImmutableList.builder();
for (int i = 0; i < PSEUDORANDOM_DATASET_SIZE; i++) {
largeDatasetBuilder.add(random.nextGaussian());
}
return largeDatasetBuilder.build();
}
代码示例来源:origin: google/guava
@Override
protected final void setUp() throws Exception {
boolean fair = new Random().nextBoolean();
monitor = new Monitor(fair);
tearDownStack.addTearDown(thread1 = new TestThread<>(monitor, "TestThread #1"));
tearDownStack.addTearDown(thread2 = new TestThread<>(monitor, "TestThread #2"));
}
代码示例来源:origin: libgdx/libgdx
/** Returns a random number between start (inclusive) and end (inclusive). */
static public long random (long start, long end) {
return start + (long)(random.nextDouble() * (end - start));
}
代码示例来源:origin: greenrobot/greenDAO
/** {@inheritDoc} */
protected Long createRandomPk() {
return random.nextLong();
}
代码示例来源:origin: spring-projects/spring-framework
@Override
public UUID generateId() {
byte[] randomBytes = new byte[16];
this.random.nextBytes(randomBytes);
long mostSigBits = 0;
for (int i = 0; i < 8; i++) {
mostSigBits = (mostSigBits << 8) | (randomBytes[i] & 0xff);
}
long leastSigBits = 0;
for (int i = 8; i < 16; i++) {
leastSigBits = (leastSigBits << 8) | (randomBytes[i] & 0xff);
}
return new UUID(mostSigBits, leastSigBits);
}
代码示例来源:origin: square/retrofit
/**
* Randomly determine whether this call should result in an HTTP error in accordance with
* configured behavior. When true, {@link #createErrorResponse()} should be returned.
*/
public boolean calculateIsError() {
return random.nextInt(100) < errorPercent;
}
代码示例来源:origin: google/guava
public void testCorrectOrdering_mediumHeapsPollFirst() {
for (int attempts = 0; attempts < reduceIterationsIfGwt(5000); attempts++) {
int size = new Random().nextInt(256) + 16;
ArrayList<Integer> elements = createOrderedList(size);
List<Integer> expected = ImmutableList.copyOf(elements);
MinMaxPriorityQueue<Integer> q = MinMaxPriorityQueue.create();
long seed = insertRandomly(elements, q);
while (!q.isEmpty()) {
elements.add(q.pollFirst());
}
assertEqualsUsingSeed(seed, expected, elements);
}
}
代码示例来源:origin: bumptech/glide
private Write getOffsetBufferWrite(Random random) {
int dataSize = random.nextInt(MAX_BYTES_PER_WRITE * 2);
byte[] data = new byte[dataSize];
int length = dataSize == 0 ? 0 : random.nextInt(dataSize);
int offset = dataSize - length <= 0 ? 0 : random.nextInt(dataSize - length);
random.nextBytes(data);
return new Write(data, length, offset, WriteType.OFFSET_BUFFER);
}
代码示例来源:origin: square/okhttp
/**
* Uses {@code request} to connect a new web socket.
*/
@Override public WebSocket newWebSocket(Request request, WebSocketListener listener) {
RealWebSocket webSocket = new RealWebSocket(request, listener, new Random(), pingInterval);
webSocket.connect(this);
return webSocket;
}
内容来源于网络,如有侵权,请联系作者删除!