Random Everything¶
Faker provides its wrapper functions around java.util.Random
(with some additional functionality that is not covered by java.util.Random
) through Faker().random
property.
Wrappers around java.util.Random
¶
Random Enum Instance¶
Random Strings¶
Random sub-lists and sub-sets¶
val list = List(100) { it }
faker.random.randomSublist(list, size = 10, shuffled = true)
faker.random.randomSublist(list, sizeRange = 6..42, shuffled = true)
val set = setOf(*List(100) { it }.toTypedArray())
faker.random.randomSubset(set, size = 10, shuffled = true)
faker.random.randomSubset(set, sizeRange = 66..99, shuffled = true)
Random element from a list/array¶
Random UUID¶
Unique Random Values¶
Just like most data providers, Faker#random
supports generation of unique values. See Generating Unique Values page for usage details.
Both "local" (data-generator level) and "global" (faker level) generation of unique values are supported for RandomProvider
:
val ints = List(21) {
faker.random.unique.nextInt(42)
}
assert(ints.distinct().size == 21)
// cleanup of unique values via enum key for nextInt function
faker.random.unique.clear(RandomProvider.Key.NEXT_INT)
faker.unique.configuration { enable(faker::random) }
val uniqueInts = List(21) {
faker.random.nextInt(42)
}
assert(uniqueInts.distinct().size == 21)
// cleanup global unique values for Random provider
faker.unique.clear(faker::random)
// disable global unique values for Random provider
faker.unique.configuration { disable(faker::random) }
val ints = List(21) {
faker.random.nextInt(42)
}
assert(ints.distinct().size < 21)