Returns a random element of the range, using the given generator as
a source for randomness.
You can use this method to select a random element of a range when you
are using a custom random number generator. If you're generating a random
number, in most cases, you should prefer using the random(in:)
static method of the desired numeric type. That static method is available
for both integer and floating point types, and returns a non-optional
value.
This method uses the default random generator, Random.default
. Calling
(1..<100).randomElement()
is equivalent to calling
(1..<100).randomElement(using: &Random.default)
.
Returns: A random element of the range.
If the range is empty, the method returns nil
.
Declaration
func randomElement() -> Range<Bound>.Element?
Declared In
Range
,
RandomAccessCollection
,
BidirectionalCollection
,
Collection