2222import java .util .List ;
2323import java .util .Set ;
2424import java .util .concurrent .ThreadLocalRandom ;
25+ import java .util .function .IntUnaryOperator ;
2526
2627import org .apache .commons .lang3 .ArrayUtils ;
2728import org .apache .commons .lang3 .StringUtils ;
@@ -118,7 +119,7 @@ public static class Builder implements org.apache.commons.text.Builder<RandomStr
118119 /**
119120 * The source of randomness.
120121 */
121- private TextRandomProvider random ;
122+ private IntUnaryOperator random ;
122123
123124 /**
124125 * The source of provided characters.
@@ -216,7 +217,42 @@ public Builder selectFrom(final char... chars) {
216217 * be used to provide the random number generation.
217218 *
218219 * <p>
219- * When using Java 8 or later, {@link TextRandomProvider} is a
220+ * {@link TextRandomProvider} is a
221+ * functional interface and need not be explicitly implemented:
222+ * </p>
223+ * <pre>
224+ * {@code
225+ * UniformRandomProvider rng = RandomSource.create(...);
226+ * RandomStringGenerator gen = RandomStringGenerator.builder()
227+ * .usingRandom(rng::nextInt)
228+ * // additional builder calls as needed
229+ * .build();
230+ * }
231+ * </pre>
232+ *
233+ * <p>
234+ * Passing {@code null} to this method will revert to the default source of
235+ * randomness.
236+ * </p>
237+ *
238+ * @param random
239+ * the source of randomness, may be {@code null}
240+ * @return {@code this}, to allow method chaining
241+ * @since 1.14.0
242+ */
243+ public Builder usingRandom (final IntUnaryOperator random ) {
244+ this .random = random ;
245+ return this ;
246+ }
247+
248+ /**
249+ * Overrides the default source of randomness. It is highly
250+ * recommended that a random number generator library like
251+ * <a href="https://commons.apache.org/proper/commons-rng/">Apache Commons RNG</a>
252+ * be used to provide the random number generation.
253+ *
254+ * <p>
255+ * {@link TextRandomProvider} is a
220256 * functional interface and need not be explicitly implemented:
221257 * </p>
222258 * <pre>
@@ -334,7 +370,7 @@ public static Builder builder() {
334370 /**
335371 * The source of randomness for this generator.
336372 */
337- private final TextRandomProvider random ;
373+ private final IntUnaryOperator random ;
338374
339375 /**
340376 * The source of provided characters.
@@ -355,7 +391,7 @@ public static Builder builder() {
355391 * @param characterList list of predefined set of characters.
356392 */
357393 private RandomStringGenerator (final int minimumCodePoint , final int maximumCodePoint ,
358- final Set <CharacterPredicate > inclusivePredicates , final TextRandomProvider random ,
394+ final Set <CharacterPredicate > inclusivePredicates , final IntUnaryOperator random ,
359395 final List <Character > characterList ) {
360396 this .minimumCodePoint = minimumCodePoint ;
361397 this .maximumCodePoint = maximumCodePoint ;
0 commit comments