public String getRandomWord ( final int minLength , final int maxLength ) { validateMinMaxParams ( minLength , maxLength ) ; // special case if we need a single char if ( maxLength == 1 ) { if ( chance ( 50 ) ) { return "a" ; } return "I" ; } // start from random pos and find the first word of the right size String [ ] words = contentDataValues . getWords ( ) ; int pos = random . nextInt ( words . length ) ; for ( int i = 0 ; i < words . length ; i ++ ) { int idx = ( i + pos ) % words . length ; String test = words [ idx ] ; if ( test . length ( ) >= minLength && test . length ( ) <= maxLength ) { return test ; } } // we haven't a word for this length so generate one return getRandomChars ( minLength , maxLength ) ; }
Returns a valid word based on the length range passed in . The length will always be between the min and max length range inclusive .