Skip to content

Instantly share code, notes, and snippets.

@kandelvijaya
Created November 27, 2016 18:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kandelvijaya/351c84ad51e22cbf25eb35c617ad72ba to your computer and use it in GitHub Desktop.
Save kandelvijaya/351c84ad51e22cbf25eb35c617ad72ba to your computer and use it in GitHub Desktop.
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