Skip to content

Instantly share code, notes, and snippets.

@skreutzberger
Created August 12, 2015 14:17
Show Gist options
  • Save skreutzberger/4e295f9d222e797649da to your computer and use it in GitHub Desktop.
Save skreutzberger/4e295f9d222e797649da to your computer and use it in GitHub Desktop.
generate random email adddress in Swift
// returns a random email as string
func randomEmail() -> String {
let nameLength = randomInt(5, to: 10)
let domainLength = randomInt(5, to: 10)
let domainSuffixes = ["com", "net", "org", "io", "co.uk"]
let name = randomText(nameLength, justLowerCase: true)
let domain = randomText(domainLength, justLowerCase: true)
let randomDomainSuffixIndex = Int(arc4random_uniform(UInt32(domainSuffixes.count)))
let domainSuffix = domainSuffixes[randomDomainSuffixIndex]
let text = name + "@" + domain + "." + domainSuffix
return text
}
@alamodey
Copy link

This doesn't work.

@dphans
Copy link

dphans commented Jun 5, 2021

I contribute a little...

extension String {
    
    func getRandomEmail(currentStringAsUsername: Bool = false) -> String {
        let providers = ["gmail.com", "hotmail.com", "icloud.com", "live.com"]
        let randomProvider = providers.randomElement()!
        if currentStringAsUsername && self.count > 0 {
            return "\(self)@\(randomProvider)"
        }
        let username = UUID.init().uuidString.replacingOccurrences(of: "-", with: "")
        return "\(username)@\(randomProvider)"
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment