java.util.Random和Java中的递归

Jus*_*yle 1 java recursion

我想首先说这是一个更普遍的问题; 没有一个与我给出的具体例子有关,而只是一个概念性的话题.

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

Gist URL(我是StackExchange的新手 - 抱歉!)

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

}
Run Code Online (Sandbox Code Playgroud)

示例#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;
    }

}
Run Code Online (Sandbox Code Playgroud)

重点:这是做这样事情最有效的方法吗?请记住,我给了你一些例子,所以它们很模糊.我不希望使用任何库,但如果它们确实在效率上有很大差异,请让我知道它们.

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

And*_*eas 6

我建议你生成ID生成的ID序列号,而不是双打或uuids.如果您希望它们对最终用户显示为随机,请在base64中显示该数字的sha1.