You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by ah...@apache.org on 2019/08/06 10:42:29 UTC

[commons-rng] 06/07: RNG-85: Allow primitive types to correctly seed the MSWS

This is an automated email from the ASF dual-hosted git repository.

aherbert pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-rng.git

commit 45bb52996ebc089cfdadb0bf32c2eb4a5419bc7c
Author: aherbert <ah...@apache.org>
AuthorDate: Mon Aug 5 16:20:39 2019 +0100

    RNG-85: Allow primitive types to correctly seed the MSWS
---
 .../rng/simple/internal/ProviderBuilder.java       | 26 ++++++++++++++++++++--
 1 file changed, 24 insertions(+), 2 deletions(-)

diff --git a/commons-rng-simple/src/main/java/org/apache/commons/rng/simple/internal/ProviderBuilder.java b/commons-rng-simple/src/main/java/org/apache/commons/rng/simple/internal/ProviderBuilder.java
index 0487b82..00e6524 100644
--- a/commons-rng-simple/src/main/java/org/apache/commons/rng/simple/internal/ProviderBuilder.java
+++ b/commons-rng-simple/src/main/java/org/apache/commons/rng/simple/internal/ProviderBuilder.java
@@ -253,8 +253,30 @@ public final class ProviderBuilder {
              NativeSeedType.LONG_ARRAY) {
             @Override
             Object createSeed() {
-                // This generator requires a high quality Weyl increment.
-                final long seed = SeedFactory.createLong();
+                return createMswsSeed(SeedFactory.createLong());
+            }
+
+            @Override
+            Object convertSeed(Object seed) {
+                // Allow seeding with primitives to generate a good seed
+                if (seed instanceof Integer) {
+                    return createMswsSeed((Integer) seed);
+                } else if (seed instanceof Long) {
+                    return createMswsSeed((Long) seed);
+                }
+                // Other types (e.g. the native long[]) are handled by the default conversion
+                return super.convertSeed(seed);
+            }
+
+            /**
+             * Creates the full length seed array from the input seed using the method
+             * recommended for the generator. This is a high quality Weyl increment composed
+             * of a hex character permutation.
+             *
+             * @param seed the seed
+             * @return the seed array
+             */
+            private long[] createMswsSeed(long seed) {
                 final long increment = SeedUtils.createLongHexPermutation(new SplitMix64(seed));
                 // The initial state should not be low complexity but the Weyl
                 // state can be any number.