Buggy code:
public class MockRandomCodec extends Codec { private final Random seedRandom ; private final String SEED_EXT = "sd" ; public MockRandomCodec ( Random random ) { } private static class MockIntStreamFactory extends IntStreamFactory { private final int salt ; private final List < IntStreamFactory > delegates = new ArrayList < IntStreamFactory > ( ) ; public MockIntStreamFactory ( Random random ) { } private static String getExtension ( String fileName ) { } @ Override public IntIndexInput openInput ( Directory dir , String fileName , int readBufferSize ) throws IOException { } @ Override public IntIndexOutput createOutput ( Directory dir , String fileName ) throws IOException { } } @ Override public FieldsConsumer fieldsConsumer ( SegmentWriteState state ) throws IOException { int skipInterval = _TestUtil . nextInt ( seedRandom , 2 , 10 ) ; if ( LuceneTestCase . VERBOSE ) { System . out . println ( ( "MockRandomCodec:<seq2seq4repair_space>skipInterval=" + skipInterval ) ) ; } final long seed = seedRandom . nextLong ( ) ; if ( LuceneTestCase . VERBOSE ) { System . out . println ( ( ( ( "MockRandomCodec:<seq2seq4repair_space>writing<seq2seq4repair_space>to<seq2seq4repair_space>seg=" + ( state . segmentName ) ) + "<seq2seq4repair_space>seed=" ) + seed ) ) ; } final String seedFileName = IndexFileNames . segmentFileName ( state . segmentName , state . codecId , SEED_EXT ) ; final IndexOutput out = state . directory . createOutput ( seedFileName ) ; out . writeLong ( seed ) ; out . close ( ) ; final Random random = new Random ( seed ) ; random . nextInt ( ) ; PostingsWriterBase postingsWriter ; if ( random . nextBoolean ( ) ) { postingsWriter = new org . apache . lucene . index . codecs . sep . SepPostingsWriterImpl ( state , new MockRandomCodec . MockIntStreamFactory ( random ) , skipInterval ) ; } else { if ( LuceneTestCase . VERBOSE ) { System . out . println ( "MockRandomCodec:<seq2seq4repair_space>writing<seq2seq4repair_space>Standard<seq2seq4repair_space>postings" ) ; } postingsWriter = new org . apache . lucene . index . codecs . standard . StandardPostingsWriter ( state , skipInterval ) ; } if ( random . nextBoolean ( ) ) { final int totTFCutoff = _TestUtil . nextInt ( random , 1 , 20 ) ; if ( LuceneTestCase . VERBOSE ) { System . out . println ( ( "MockRandomCodec:<seq2seq4repair_space>pulsing<seq2seq4repair_space>postings<seq2seq4repair_space>with<seq2seq4repair_space>totTFCutoff=" + totTFCutoff ) ) ; } postingsWriter = new org . apache . lucene . index . codecs . pulsing . PulsingPostingsWriterImpl ( totTFCutoff , postingsWriter ) ; } final TermsIndexWriterBase indexWriter ; boolean success = false ; try { if ( random . nextBoolean ( ) ) { state . termIndexInterval = _TestUtil . nextInt ( random , 1 , 100 ) ; if ( LuceneTestCase . VERBOSE ) { System . out . println ( ( ( "MockRandomCodec:<seq2seq4repair_space>fixed-gap<seq2seq4repair_space>terms<seq2seq4repair_space>index<seq2seq4repair_space>(tii=" + ( state . termIndexInterval ) ) + ")" ) ) ; } indexWriter = new org . apache . lucene . index . codecs . FixedGapTermsIndexWriter ( state ) ; } else { final VariableGapTermsIndexWriter . IndexTermSelector selector ; final int n2 = random . nextInt ( 3 ) ; if ( n2 == 0 ) { final int tii = _TestUtil . nextInt ( random , 1 , 100 ) ; selector = new VariableGapTermsIndexWriter . EveryNTermSelector ( tii ) ; if ( LuceneTestCase . VERBOSE ) { System . out . println ( ( ( "MockRandomCodec:<seq2seq4repair_space>variable-gap<seq2seq4repair_space>terms<seq2seq4repair_space>index<seq2seq4repair_space>(tii=" + tii ) + ")" ) ) ; } } else if ( n2 == 1 ) { final int docFreqThresh = _TestUtil . nextInt ( random , 2 , 100 ) ; final int tii = _TestUtil . nextInt ( random , 1 , 100 ) ; selector = new VariableGapTermsIndexWriter . EveryNOrDocFreqTermSelector ( docFreqThresh , tii ) ; } else { final long seed2 = random . nextLong ( ) ; final int gap = _TestUtil . nextInt ( random , 2 , 40 ) ; if ( LuceneTestCase . VERBOSE ) { System . out . println ( ( ( "MockRandomCodec:<seq2seq4repair_space>random-gap<seq2seq4repair_space>terms<seq2seq4repair_space>index<seq2seq4repair_space>(max<seq2seq4repair_space>gap=" + gap ) + ")" ) ) ; } selector = new VariableGapTermsIndexWriter . IndexTermSelector ( ) { final Random rand = new Random ( seed2 ) ; @ Override public boolean isIndexTerm ( BytesRef term , TermStats stats ) { <START_BUG> return ( rand . nextInt ( gap ) ) == 17 ; <END_BUG> } @ Override public void newField ( FieldInfo fieldInfo ) { } } ; } indexWriter = new VariableGapTermsIndexWriter ( state , selector ) ; } success = true ; } finally { if ( ! success ) { postingsWriter . close ( ) ; } } success = false ; try { FieldsConsumer ret = new org . apache . lucene . index . codecs . BlockTermsWriter ( indexWriter , state , postingsWriter ) ; success = true ; return ret ; } finally { if ( ! success ) { try { postingsWriter . close ( ) ; } finally { indexWriter . close ( ) ; } } } } @ Override public FieldsProducer fieldsProducer ( SegmentReadState state ) throws IOException { } @ Override public void files ( Directory dir , SegmentInfo segmentInfo , String codecId , Set < String > files ) throws IOException { } @ Override public void getExtensions ( Set < String > extensions ) { } }

Fix:
return ( rand . nextInt ( gap ) ) == ( gap // 2 ) ;