jmckenzie-dev commented on code in PR #4030:
URL: https://github.com/apache/cassandra/pull/4030#discussion_r2025513498


##########
test/unit/org/apache/cassandra/utils/RandomHelpers.java:
##########
@@ -0,0 +1,212 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.cassandra.utils;
+
+import java.math.BigDecimal;
+import java.math.BigInteger;
+import java.nio.ByteBuffer;
+import java.util.Random;
+import java.util.UUID;
+
+import com.google.common.base.Preconditions;
+import org.apache.commons.lang3.NotImplementedException;
+import org.apache.commons.math3.random.MersenneTwister;
+import org.apache.commons.math3.random.RandomGenerator;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import org.quicktheories.core.DetatchedRandomnessSource;
+import org.quicktheories.core.RandomnessSource;
+import org.quicktheories.impl.Constraint;
+
+import static org.apache.cassandra.config.CassandraRelevantEnv.USER_SEED;
+
+/**
+ * Provides a more uniform generator w/a longer period, replacability, and 
some convenience methods around seeds for reproducibility.
+ */
+@SuppressWarnings("JavadocReference")
+public final class RandomHelpers
+{
+    private static final Logger logger = 
LoggerFactory.getLogger(RandomHelpers.class);
+    private static long seed = Clock.Global.currentTimeMillis();
+    private static RandomGenerator rg = new MersenneTwister(seed);
+    private static final SimpleRandomSource srs = new SimpleRandomSource(seed);
+
+    @SuppressWarnings("unused")
+    public static void replaceRandomGenerator(RandomGenerator newGen)
+    {
+        rg = newGen;
+    }
+
+    /**
+     * Will set the seed to the user defined value in env var {@link 
#USER_SEED} if set, otherwise current time in millis
+     */
+    public static void setSeed()
+    {
+        long newSeed = USER_SEED.getString() != null
+               ? Long.parseLong(USER_SEED.getString())
+               : Clock.Global.currentTimeMillis();
+        setSeed(newSeed);
+    }
+
+    public static void setSeed(long newSeed)
+    {
+        rg.setSeed(newSeed);
+        srs.setSeed(newSeed);
+    }
+
+    public static void printSeed(String context)
+    {
+        logger.info("Random seed for context: " + context + ": " + seed);

Review Comment:
   So this is a last-minute tweak on my part since I had to move from a newer 
version of qt to older; I think we should actually update `setSeed()` as 
follows:
   ```java
       public static void setSeed(long newSeed)
       {
           seed = newSeed; // added
           rg.setSeed(newSeed);
           srs.setSeed(newSeed);
       }
   ```
   i.e. have the seed local in the class match whatever someone authoritatively 
set the internal gen to.



-- 
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: pr-unsubscr...@cassandra.apache.org

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


---------------------------------------------------------------------
To unsubscribe, e-mail: pr-unsubscr...@cassandra.apache.org
For additional commands, e-mail: pr-h...@cassandra.apache.org

Reply via email to