jbertram commented on code in PR #5487:
URL: https://github.com/apache/activemq-artemis/pull/5487#discussion_r1951998375


##########
artemis-commons/src/main/java/org/apache/activemq/artemis/utils/RandomUtil.java:
##########
@@ -31,17 +31,56 @@ public static Random getRandom() {
       return random;
    }
 
+   private static String letters = "abcdefghijklmnopqrstuvwxyz";
+
+   private static String digits = "0123456789";
+
+   private static String randomBase = letters + letters.toUpperCase() + digits;
+
+   /**
+    * Utility method to build a {@code String} filled with random 
alpha-numeric characters. The {@code String} will
+    * contain characters from the following:
+    * <ul>
+    *    <li>abcdefghijklmnopqrstuvwxyz</li>
+    *    <li>ABCDEFGHIJKLMNOPQRSTUVWXYZ</li>
+    *    <li>0123456789</li>
+    * </ul>
+    * @param length how long the returned {@code String} should be
+    * @return a {@code String} of random alpha-numeric characters
+    */
+   public static String randomAlphaNumericString(int length) {
+      StringBuilder result = new StringBuilder(length);
+      for (int i = 0; i < length; i++) {
+         result.append(randomBase.charAt(randomInterval(0, 
randomBase.length())));

Review Comment:
   I'm not following. In this case the values passed to `randomInterval` are 
essentially fixed at `0` and `62`. Ultimately `randomInterval` will invoke 
`java.util.Random#nextInt(int)` and the JavaDoc states:
   
   > Returns a pseudorandom, uniformly distributed int value between 0 
(inclusive) and the specified value (exclusive)...
   
   As far as I can tell the key here is that the specified value is 
_exclusive_. Therefore, the returned value will always be within the bounds of 
`randomBase` so any character can be selected.
   
   As a POC I write this simple test in 
`org.apache.activemq.artemis.tests.unit.core.util.RandomUtilTest`:
   ```java
      @Test
      public void testAlphaNumeric() {
         for (int i = 0; i < 1_000_000; i++) {
            RandomUtil.randomAlphaNumericString(1000);
         }
      }
   ```
   There were no occurrences of `StringIndexOutOfBoundsException`.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: gitbox-unsubscr...@activemq.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: gitbox-unsubscr...@activemq.apache.org
For additional commands, e-mail: gitbox-h...@activemq.apache.org
For further information, visit: https://activemq.apache.org/contact


Reply via email to