Skip to content

Instantly share code, notes, and snippets.

@KevinVitale
Last active February 12, 2019 03:49
Show Gist options
  • Save KevinVitale/9a599d071975e3bc908f9c7d8d35dcae to your computer and use it in GitHub Desktop.
Save KevinVitale/9a599d071975e3bc908f9c7d8d35dcae to your computer and use it in GitHub Desktop.
Generate Random Data (Swift)
print(Data.random(0xF).hexString())
// Possible Output:
// D6 33 7F 6A B3 D7 A6 B2 E9 24 12 79 73 C4 C1 E5
extension Data {
static func random(_ count: Int) -> Data {
return Data((0...count).map { _ in
UInt8.random(in: 0...UInt8.max)
})
}
func hexString(separator: String = " ") -> String {
return map { String($0, radix: 16, uppercase: true) }.joined(separator: separator)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment