echauchot commented on a change in pull request #4143: Faster implementation of 
nexmark.Generator.nextExactString().
URL: https://github.com/apache/beam/pull/4143#discussion_r152771825
 
 

 ##########
 File path: 
sdks/java/nexmark/src/main/java/org/apache/beam/sdk/nexmark/sources/Generator.java
 ##########
 @@ -353,8 +353,16 @@ private static String nextString(Random random, int 
maxLength) {
   /** Return a random string of exactly {@code length}. */
   private static String nextExactString(Random random, int length) {
     StringBuilder sb = new StringBuilder();
+    int rnd = 0;
+    int n = 0;  // number of random characters left in rnd
     while (length-- > 0) {
-      sb.append((char) ('a' + random.nextInt(26)));
+      if (n == 0) {
+        rnd = random.nextInt();
+        n = 6;  // log_26(2^31)
+      }
+      sb.append((char) ('a' + rnd % 26));
+      rnd /= 26;
 
 Review comment:
   We will concatenate ints between 0 and 25 as before. But if `rnd` happens to 
be < 26 then it will be set in this line to 0 for the next `n` iterations (6 at 
worse) leading to appending 0 to `sb`.  So there will be a more probable 0 than 
previous implementation. Consider using `Random.intervalNextInt(length*26, 
Integer.MAX_VALUE)`. But it might be not a big deal

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
[email protected]


With regards,
Apache Git Services

Reply via email to