Commits

Taku Miyakawa committed 59f51ae

Fixes the bug: empty strings and empty arrays of bytes had not been allowed as a random seed (closing issue #479)

Comments (0)

Files changed (2)

src/main/java/org/kink_lang/kink/internal/RandomNumbers.java

      */
     @SuppressWarnings( "PMD.AvoidArrayLoops" )
     public static MersenneTwister newMersenneTwister( byte[] seed ) {
-        // Suppresses a wrong PMD warning
-        int[] seedInts = new int[ seed.length ];
+        //
+        // Length of the seed array must be >=1 because of the implementation of MersenneTwister
+        // The last element is always 0
+        int[] seedInts = new int[ seed.length + 1 ];
+
+        // Copies each byte to an element of the seed array
         for ( int index = 0 ; index < seed.length ; ++ index ) {
             seedInts[ index ] = seed[ index ];
         }
+
         return new MersenneTwister( seedInts );
     }
 
      *     New RNG.
      */
     public static MersenneTwister newMersenneTwister( String seed ) {
-        int[] seedInts = new int[ seed.length() ];
+
+        // Length of the seed array must be >=1 because of the implementation of MersenneTwister
+        // The last element is always 0
+        int[] seedInts = new int[ seed.length() + 1 ];
+
+        // Copies each character to an element of the seed array
         for ( int index = 0 ; index < seed.length() ; ++ index ) {
             seedInts[ index ] = seed.charAt( index );
         }
+
         return new MersenneTwister( seedInts );
     }
 

src/test/java/org/kink_lang/kink/internal/RandomNumbersTest.java

 
 
     @Test
+    public void make_mersenne_twister_rng_with_empty_string_seed() {
+        MersenneTwister mtX = RandomNumbers.newMersenneTwister( "" );
+        MersenneTwister mtY = RandomNumbers.newMersenneTwister( "" );
+        assertThat( mtX.nextInt() , is( mtY.nextInt() ) );
+        assertThat( mtX.nextInt() , is( mtY.nextInt() ) );
+        assertThat( mtX.nextInt() , is( mtY.nextInt() ) );
+        assertThat( mtX.nextInt() , is( mtY.nextInt() ) );
+        assertThat( mtX.nextInt() , is( mtY.nextInt() ) );
+    }
+
+
+    @Test
     public void make_mersenne_twister_rng_with_byte_array_seed() {
         MersenneTwister mtX = RandomNumbers.newMersenneTwister( new byte[] { 1 , 2 , 3 } );
         MersenneTwister mtY = RandomNumbers.newMersenneTwister( new byte[] { 1 , 2 , 3 } );
 
 
     @Test
+    public void make_mersenne_twister_rng_with_empty_byte_array_seed() {
+        MersenneTwister mtX = RandomNumbers.newMersenneTwister( new byte[ 0 ] );
+        MersenneTwister mtY = RandomNumbers.newMersenneTwister( new byte[ 0 ] );
+        assertThat( mtX.nextInt() , is( mtY.nextInt() ) );
+        assertThat( mtX.nextInt() , is( mtY.nextInt() ) );
+        assertThat( mtX.nextInt() , is( mtY.nextInt() ) );
+        assertThat( mtX.nextInt() , is( mtY.nextInt() ) );
+        assertThat( mtX.nextInt() , is( mtY.nextInt() ) );
+    }
+
+
+    @Test
     public void make_mersenne_twister_rng_with_default_seed() {
         MersenneTwister mt = RandomNumbers.newMersenneTwister();
         mt.nextInt();