The following generators based on Xor shifts all require non-zero seeds:
A zero seed results in zero output for each call to the generator.
Tests added to the rng-core module show that only a single bit is required to be set and the generator is able to generate non-zero output. These tests are simple and do not test the output for many cycles or do statistical analysis on the output randomness. However the authors of the generators state that they require a non-zero seed and are not any more specific so I assume that a single bit is enough to maintain the internal state capable of randomness.
This can be fixed with the following methods:
- Set the final bit in the seed to 1. This will be fast but loses 1 bit of seed entropy.
- Check the seed array for zeros. If the entire array is zero then generate a single seed value until it is non-zero in a loop and set it into the seed array.
- Check the seed array for zeros. If the entire array is zero then re-generate the seed using recursive method call.
Note that this is an edge case. The smallest seed uses 64 bits, others use 128, 256, 512, 1024 (the number of bits in the seed is the state size and is the number in the enum). For the worst case this will occur 1 in 2^64 times.
Here is the robust approach:
This would add new isZero(int) and isZero(long) methods to the seed factory. The entire method for creating a non-zero array seed could be added to the SeedFactory.
Here is the compromise approach:
Given the loop will happen 1 in 2^64 times the speed of the two approaches will differ due to how the JVM produces the final code for the hot path (i.e. not the edge case).
Here is the simple approach:
Losing 1 bit of entropy is undesirable.
A performance test of the approaches (and variants) should be done for reference.