Permalink
Show file tree
Hide file tree
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
RNG-174: Improve support for non-zero seeds
Allow specification of a range for array seeds that must be non-zero. The values for the range are based on the result of o.a.c.rng.simple.ProvidersCommonParametricTest.testZeroIntArraySeed. See also the core module for tests using: - RandomAssert.assertNextIntZeroOutput - RandomAssert.assertIntArrayConstructorWithSingleBitInPoolIsFunctional Any generator that fails these tests requires a non-zero seed. In most cases this was set as the full seed length, or one less for generators that do not use all the bits of the seed array (WELL_19937_x, WELL_44497_x). Notable exceptions: The KISS generator is reduced to a simple LCG when positions [0, 3) are all zero. Added a test to demonstrate this. With a zero seed the KISS LCG passes testZeroIntArraySeed. To avoid a poor generator the seed will be checked to be non-zero in [0, 3). The MSWS generator is sensitive to the initial state. Added a test to show that a zero seed creates zero output. Updating RandomAssert to add an assertLongArrayConstructorWithSingleBitInPoolIsFunctional test shows the MSWS fails with single bit seeds. The sub-range has been set to [2, 3) to ensure a non-zero Weyl increment which is the best way to escape a bad seed. This is a functionally breaking change.
- Loading branch information
Showing
14 changed files
with
800 additions
and
175 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@@ -0,0 +1,73 @@ | ||
/* | ||
* 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.commons.rng.simple.internal; | ||
|
||
/** | ||
* Performs mixing of bits. | ||
* | ||
* @since 1.5 | ||
*/ | ||
final class MixFunctions { | ||
/** | ||
* The fractional part of the the golden ratio, phi, scaled to 64-bits and rounded to odd. | ||
* This can be used as an increment for a Weyl sequence. | ||
* | ||
* @see <a href="https://en.wikipedia.org/wiki/Golden_ratio">Golden ratio</a> | ||
*/ | ||
static final long GOLDEN_RATIO_64 = 0x9e3779b97f4a7c15L; | ||
/** | ||
* The fractional part of the the golden ratio, phi, scaled to 32-bits and rounded to odd. | ||
* This can be used as an increment for a Weyl sequence. | ||
* | ||
* @see <a href="https://en.wikipedia.org/wiki/Golden_ratio">Golden ratio</a> | ||
*/ | ||
static final int GOLDEN_RATIO_32 = 0x9e3779b9; | ||
|
||
/** No instances. */ | ||
private MixFunctions() {} | ||
|
||
/** | ||
* Perform variant 13 of David Stafford's 64-bit mix function. | ||
* This is the mix function used in the | ||
* {@link org.apache.commons.rng.core.source64.SplitMix64 SplitMix64} RNG. | ||
* | ||
* <p>This is ranked first of the top 14 Stafford mixers. | ||
* | ||
* @param x the input value | ||
* @return the output value | ||
* @see <a href="http://zimbry.blogspot.com/2011/09/better-bit-mixing-improving-on.html">Better | ||
* Bit Mixing - Improving on MurmurHash3's 64-bit Finalizer.</a> | ||
*/ | ||
static long stafford13(long x) { | ||
x = (x ^ (x >>> 30)) * 0xbf58476d1ce4e5b9L; | ||
x = (x ^ (x >>> 27)) * 0x94d049bb133111ebL; | ||
return x ^ (x >>> 31); | ||
} | ||
|
||
/** | ||
* Perform the finalising 32-bit mix function of Austin Appleby's MurmurHash3. | ||
* | ||
* @param x the input value | ||
* @return the output value | ||
* @see <a href="https://github.com/aappleby/smhasher">SMHasher</a> | ||
*/ | ||
static int murmur3(int x) { | ||
x = (x ^ (x >>> 16)) * 0x85ebca6b; | ||
x = (x ^ (x >>> 13)) * 0xc2b2ae35; | ||
return x ^ (x >>> 16); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.