GeneratePassword.swift
import Foundation
private let characters = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
func generateRandomString(_ length: Int) -> String {
// Start with an empty string
var string = ""
// Append 'length' number of random characters
for _ in 0..<length {
string.append(generateRandomCharacter())
}
print("Random String: string= \(string)")
return string
}
func generateRandomCharacter() -> Character {
// Create a random index into the characters array
let index: Int = Int(arc4random_uniform(UInt32(characters.characters.count)))
var stringIndex: String.CharacterView.Index = characters.characters.startIndex
stringIndex = characters.index(stringIndex, offsetBy: index)
// Get and return a random character
let character = characters[stringIndex]
print("Random Character: index= \(index)\tcharacter= \(character)")
return character
}