[英]Is there a randomize function that returns values of a chosen type?
Is there a way to use a randomize function with relation to a give data type, ie if I request the type is double
then I will get double
values, and if the requested type is float
I will get float
values? 有没有一种方法可以使用与给定数据类型相关的随机函数,即如果我请求的类型为
double
那么我将获得double
float
值;如果请求的类型为float
我将获得float
值?
Is there such a randomize function? 有这样的随机功能吗?
java 1.7: Java 1.7:
Either you call ThreadLocalRandom.current().next<DataType>()
by yourself, or you write a wrapper around this call: 您可以自己调用
ThreadLocalRandom.current().next<DataType>()
,或者在此调用周围编写包装器:
import java.util.concurrent.ThreadLocalRandom;
//...
public static int nextRandom(int maxValueExclusive)
{
return ThreadLocalRandom.current().nextInt(maxValueExclusive);
}
public static long nextRandom(long maxValueExclusive)
{
return ThreadLocalRandom.current().nextLong(maxValueExclusive);
}
public static double nextRandom(double maxValueExclusive)
{
return ThreadLocalRandom.current().nextDouble(maxValueExclusive);
}
public static float nextRandom(float maxValueExclusive)
{
if (maxValueExclusive <= 0)
throw new IllegalArgumentException("argument must be positive: " + maxValueExclusive);
return ThreadLocalRandom.current().nextFloat()*maxValueExclusive;
}
public static boolean nextRandom(boolean unusedValue)
{
return ThreadLocalRandom.current().nextBoolean();
}
java 1.6: Java 1.6:
import java.util.Random;
//...
private static final Random random = new Random(); // be careful with multiple threads
public static int nextRandom(int maxValueExclusive)
{
return random.nextInt(maxValueExclusive);
}
public static long nextRandom(long maxValueExclusive)
{
//implementation from java 1.7 ThreadLocalRandom
if (maxValueExclusive <= 0)
throw new IllegalArgumentException("argument must be positive: " + maxValueExclusive);
// Divide n by two until small enough for nextInt. On each
// iteration (at most 31 of them but usually much less),
// randomly choose both whether to include high bit in result
// (offset) and whether to continue with the lower vs upper
// half (which makes a difference only if odd).
long offset = 0;
while (maxValueExclusive >= Integer.MAX_VALUE) {
long half = maxValueExclusive >>> 1;
long nextn = random.nextBoolean() ? half : maxValueExclusive - half;
if (random.nextBoolean())
offset += maxValueExclusive - nextn;
maxValueExclusive = nextn;
}
return offset + random.nextInt((int) maxValueExclusive);
}
public static double nextRandom(double maxValueExclusive)
{
if (maxValueExclusive <= 0)
throw new IllegalArgumentException("argument must be positive: " + maxValueExclusive);
return random.nextDouble()*maxValueExclusive;
}
public static float nextRandom(float maxValueExclusive)
{
if (maxValueExclusive <= 0)
throw new IllegalArgumentException("argument must be positive: " + maxValueExclusive);
return random.nextFloat()*maxValueExclusive;
}
public static boolean nextRandom(boolean unusedValue)
{
return random.nextBoolean();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.