From e4aecc1b476dcfeeb9f9d5874f0c3595f88e09a3 Mon Sep 17 00:00:00 2001 From: Chris Larsen Date: Wed, 2 Aug 2017 23:45:03 -0700 Subject: [PATCH] [core] Add a Fisher-Yates array shuffle to the Utils class. --- core/src/main/java/com/yahoo/ycsb/Utils.java | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/core/src/main/java/com/yahoo/ycsb/Utils.java b/core/src/main/java/com/yahoo/ycsb/Utils.java index 9a95cbd6ae..ade6a1cee3 100644 --- a/core/src/main/java/com/yahoo/ycsb/Utils.java +++ b/core/src/main/java/com/yahoo/ycsb/Utils.java @@ -226,4 +226,19 @@ public static Map getGCStatst() { } return map; } + + /** + * Simple Fisher-Yates array shuffle to randomize discrete sets. + * @param array The array to randomly shuffle. + * @return The shuffled array. + */ + public static T [] shuffleArray(final T[] array) { + for (int i = array.length -1; i > 0; i--) { + final int idx = RAND.nextInt(i + 1); + final T temp = array[idx]; + array[idx] = array[i]; + array[i] = temp; + } + return array; + } }