Last active
February 12, 2019 03:49
-
-
Save KevinVitale/9a599d071975e3bc908f9c7d8d35dcae to your computer and use it in GitHub Desktop.
Generate Random Data (Swift)
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
print(Data.random(0xF).hexString()) | |
// Possible Output: | |
// D6 33 7F 6A B3 D7 A6 B2 E9 24 12 79 73 C4 C1 E5 |
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 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