java - 是否有一个随机函数可以返回所选类型的值?

标签 java random

有没有办法使用与给定数据类型相关的随机函数,即 如果我请求的类型是 double ,那么我将得到 double 值,如果请求的类型是 float ,我将得到 float/code> 值?

有这样的随机函数吗?

最佳答案

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();
}

关于java - 是否有一个随机函数可以返回所选类型的值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14672113/

相关文章:

vba - 使用 VBA 从分布中生成随机数到内存

algorithm - 真随机数发生器

java - 如何使 TreeSelectionListener 作为 JEditorPane 上的 Hyperlinklistener 工作?

java - 如何从方法访问私有(private)变量?

java - 为什么 HTTP POST 返回代码 400(错误请求)? HTTP POST 方法

algorithm - 在小于 O(N) 的时间内生成 N 个准随机数

java - 外部排序在 readInt() 调用中给出 OutOfMemory

java - 在Java中解析管道分隔的字符串(管道可以转义)

javascript - 随机化所有字符串字符的大写/小写

random - 从列表中随机选择一个值,然后在 SOAP 请求 TestStep 中使用该值