特定のデータ型に関連してランダム化関数を使用する方法はありますか?つまり、型を要求すると値がdouble
取得double
され、要求された型がfloat
float
そのようなランダム化機能はありますか?
Java 1.7:自分で
呼び出すか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:
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();
}