Created
November 27, 2016 18:48
-
-
Save kandelvijaya/351c84ad51e22cbf25eb35c617ad72ba 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
import Foundation | |
struct RandomStringGenerator { | |
func createRandomWord(with wordLength: Int) -> String { | |
let chars = (0..<wordLength).map{_ in return String(randomChar()) } | |
return chars.reduce(""){ $0 + $1 } | |
} | |
func randomChar() -> Character { | |
let ascii = 65 + arc4random() % (122 - 65) | |
return UnicodeScalar(ascii).map { Character($0) } ?? Character("") | |
} | |
func createRandomParagraph(with wordCount: Int) -> String { | |
let maxWordLength = Int(arc4random() % 15) | |
return (0..<wordCount).map{ _ in createRandomWord(with: maxWordLength) }.reduce(""){ | |
$0 + " " + $1 | |
} | |
} | |
} | |
RandomStringGenerator().createRandomParagraph(with: 5) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment