Java 中的 java.util.Random 和递归

标签 java recursion

首先我想说这是一个更普遍的问题;与我给出的具体示例无关,而只是一个概念性主题。

示例#1: 我正在使用 UUID.java 创建一个真正随机的字符串。假设我永远不想生成相同的 UUID。这是对这种情况的一个想法: (假设我在顶部保存/加载列表 - 这不是重点)

Gist URL (I'm new to StackExchange- sorry!)

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

public class Example {

    /**
     * A final List<String> of all previous UUIDs generated with
     * generateUniqueID(), turned into a string with uuid.toString();
     */
    private static final List<String> PREVIOUS = new ArrayList<String>();

    /**
     * Generates a truly unique UUID.
     * 
     * @param previous
     *            A List<String> of previous UUIDs, converted into a string with
     *            uuid.toString();
     * @return a UUID generated with UUID.randomUUID(); that is not included in
     *         the given List<String>.
     */
    public static UUID generateUniqueID(List<String> previous) {
        UUID u = UUID.randomUUID();
        if (previous.contains(u.toString())) {
            return generateUniqueID(previous);
        }
        return u;
    }

    /**
     * Generates a truly unique UUID using the final List<String> PREVIOUS
     * variable defined at the top of the class.
     * 
     * @return A truly random UUID created with generateUniqueID(List<String>
     *         previous);
     */
    public static UUID generateUniqueID() {
        UUID u = generateUniqueID(PREVIOUS);
        PREVIOUS.add(u.toString());
        return u;
    }

}

示例 #2:好吧,也许 UUID 是一个不好的例子,所以让我们使用 Random 和 double。这是另一个例子:

Gist URL

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

public class Example2 {

    /**
     * A final List<Double> of all previous double generated with
     * generateUniqueDouble(), turned into a string with Double.valueOf(d);
     */
    private static final List<Double> PREVIOUS = new ArrayList<Double>();

    /**
     * The RANDOM variable used in the class.
     */
    private static final Random RANDOM = new Random();

    /**
     * Generates a truly unique double.
     * 
     * @param previous
     *            A List<Double> of previous doubles, converted into a Double
     *            with Double.valueOf(d);
     * @return a UUID generated with UUID.randomUUID(); that is not included in
     *         the given List<Double>.
     */
    public static double generateUniqueDouble(List<Double> previous) {
        double d = RANDOM.nextDouble();
        if (previous.contains(Double.valueOf(d))) {
            return generateUniqueDouble(previous);
        }
        return d;
    }

    /**
     * Generates a truly unique double using the final List<Double> PREVIOUS
     * variable defined at the top of the class.
     * 
     * @return A truly random double created with generateUnique(List<Double>
     *         previous);
     */
    public static double generateUnique() {
        double d = RANDOM.nextDouble();
        PREVIOUS.add(Double.valueOf(d));
        return d;
    }

}

要点:这是执行此类操作的最有效方法吗?请记住,我给你举了例子,所以它们很模糊。最好我不想为此使用任何库,但如果它们确实在效率上有很大差异,请让我知道它们。

请让我知道您在回复中的想法:)

最佳答案

我建议您制作生成的 ID 序列号,而不是 double 或 uuid。如果您希望它们对最终用户随机显示,请在 base64 中显示数字的 sha1。

关于Java 中的 java.util.Random 和递归,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29615792/

相关文章:

python - 为什么这个 python 实现的归并排序不起作用?

c - "Program received signal SIGSEGV , Segmentation fault” 尝试使用递归获取所有3字符组合的关键字时

java - JWT 示例 - "Error while requesting an Access Token"

java - 向右移动 wicket paginnavigator

java - 如何通过代码打开 Vaadin ComboBox?

java - for-each 语法转换

javascript - 警告 : Recursive process. 检测到 nextTick

java - 是否可以并行运行递归函数?

java - 递归 - 试图理解

java - 使用枚举作为参数的 Hibernate 查询