Buggy code:
public class MockRandomCodec extends Codec { private final Random seedRandom ; private final String SEED_EXT = "sd" ; public MockRandomCodec ( Random random ) { } @ Override public FieldsConsumer fieldsConsumer ( SegmentWriteState state ) throws IOException { final long seed = seedRandom . nextLong ( ) ; 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 ) ; PostingsWriterBase postingsWriter ; final int n = random . nextInt ( 4 ) ; if ( n == 0 ) { if ( LuceneTestCase . VERBOSE ) { System . out . println ( "MockRandomCodec:<seq2seq4repair_space>writing<seq2seq4repair_space>MockSep<seq2seq4repair_space>postings" ) ; } postingsWriter = new org . apache . lucene . index . codecs . sep . SepPostingsWriterImpl ( state , new MockSingleIntFactory ( ) ) ; } else if ( n == 1 ) { final int blockSize = _TestUtil . nextInt ( random , 1 , 2000 ) ; if ( LuceneTestCase . VERBOSE ) { System . out . println ( ( ( "MockRandomCodec:<seq2seq4repair_space>writing<seq2seq4repair_space>MockFixedIntBlock(" + blockSize ) + ")<seq2seq4repair_space>postings" ) ) ; } postingsWriter = new org . apache . lucene . index . codecs . sep . SepPostingsWriterImpl ( state , new MockFixedIntBlockCodec . MockIntFactory ( blockSize ) ) ; } else if ( n == 2 ) { final int baseBlockSize = _TestUtil . nextInt ( random , 1 , 127 ) ; if ( LuceneTestCase . VERBOSE ) { System . out . println ( ( ( "MockRandomCodec:<seq2seq4repair_space>writing<seq2seq4repair_space>MockVariableIntBlock(" + baseBlockSize ) + ")<seq2seq4repair_space>postings" ) ) ; } postingsWriter = new org . apache . lucene . index . codecs . sep . SepPostingsWriterImpl ( state , new MockVariableIntBlockCodec . MockIntFactory ( baseBlockSize ) ) ; } 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 ) ; } 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 ( random . 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 , BytesRef . getUTF8SortedAsUnicodeComparator ( ) ) ; 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 ) ) == 17 ;