Skip to content

Commit 7a41ee5

Browse files
committed
Fix Uuids.random to get better distribution
Before this commit it used `random.nextBytes` which made it generate an `int` and crop it to `byte`, since it is quasi random, it produces bad distribution and lead to have more UUID collisions.
1 parent 9891e31 commit 7a41ee5

File tree

1 file changed

+7
-1
lines changed
  • core/src/main/java/com/datastax/oss/driver/api/core/uuid

1 file changed

+7
-1
lines changed

Diff for: core/src/main/java/com/datastax/oss/driver/api/core/uuid/Uuids.java

+7-1
Original file line numberDiff line numberDiff line change
@@ -302,7 +302,13 @@ public static UUID random() {
302302
@NonNull
303303
public static UUID random(@NonNull Random random) {
304304
byte[] data = new byte[16];
305-
random.nextBytes(data);
305+
for (int i = 0; i < 16; i += 4) {
306+
int rnd = random.nextInt();
307+
data[i] = (byte) (rnd >>> 24);
308+
data[i + 1] = (byte) (rnd >>> 16);
309+
data[i + 2] = (byte) (rnd >>> 8);
310+
data[i + 3] = (byte) rnd;
311+
}
306312
return buildUuid(data, 4);
307313
}
308314

0 commit comments

Comments
 (0)