Skip to content

Instantly share code, notes, and snippets.

@deathlezz
Last active July 31, 2021 10:54
Show Gist options
  • Save deathlezz/092789211e460315ba9264cbea0f9444 to your computer and use it in GitHub Desktop.
Save deathlezz/092789211e460315ba9264cbea0f9444 to your computer and use it in GitHub Desktop.
Find random element in array in Swift 5.
//
// Find random element in array
//
let array = "abcdefghijklmnopqrstuvwxyz"
let random = (0...3).compactMap { _ in array.randomElement() } // (0...3) - it means amount of characters
print(random) // ["f", "u", "y", "m"]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment