使用UUID生成可重现的ID?

Jac*_*ain 0 java

我正在使用UUID.randomUUID().getLeastSignificantBits();生成唯一ID.但是,我想每次运行应用程序时生成相同的ID,以便调试我的代码.我怎样才能做到这一点?

编辑:感谢zim-zam我创建了这个解决问题的类.

public class IDGenerator {
private static Random random = new Random(1);
public static long getID() {
    long id;
    byte[] array = new byte[16];
    random.nextBytes(array);
    id = UUID.nameUUIDFromBytes( array ).getLeastSignificantBits();
    return id;
}
}
Run Code Online (Sandbox Code Playgroud)

Zim*_*oot 5

您可以使用从种子或种子UUID.nameUUIDFromBytes(byte[] bytes)到达的地方byte[] bytesRandomSecureRandom

  • 对于迟来的问题感到抱歉,但是执行 `new UUID(rng.nextLong(), rng.nextLong())` 来获得可重现的 UUID 不是也是正确的吗? (2认同)