Last active
February 3, 2024 19:31
-
-
Save sindresorhus/895971b9711f03658931bb122a94104a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
extension CaseIterable { | |
public static func randomCaseIterableElement(using generator: inout some RandomNumberGenerator) -> Self { | |
allCases.randomElement(using: &generator)! | |
} | |
public static func randomCaseIterableElement() -> Self { | |
var generator = SystemRandomNumberGenerator() | |
return randomCaseIterableElement(using: &generator) | |
} | |
} | |
enum Foo: String, CaseIterable { | |
case a | |
case b | |
case c | |
} | |
print(Foo.randomCaseIterableElement()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment