Skip to content

Instantly share code, notes, and snippets.

@TheMuellenator
Last active November 1, 2025 11:14
Show Gist options
  • Select an option

  • Save TheMuellenator/9fdb0d41d6343218ca1bcb6fb724949e to your computer and use it in GitHub Desktop.

Select an option

Save TheMuellenator/9fdb0d41d6343218ca1bcb6fb724949e to your computer and use it in GitHub Desktop.
iOS repl.it - Randomisation Challenge Solution
let alphabet = ["a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"]
//The number of letters in alphabet equals 26
var password = alphabet[Int.random(in: 0...25)] + alphabet[Int.random(in: 0...25)] + alphabet[Int.random(in: 0...25)] + alphabet[Int.random(in: 0...25)] + alphabet[Int.random(in: 0...25)] + alphabet[Int.random(in: 0...25)]
print(password)
@raphaelalmeidamartins

Copy link
Copy Markdown

It's great to see all the different solutions for the problem. Here is mine:

func exercise() {
    let alphabet = ["a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"];
    
    //The number of letters in alphabet equals 26
    var randomAlphabet = alphabet;
    randomAlphabet.shuffle();
    let password = randomAlphabet[0..<6].joined();
    
    print(password);
}

@Invader1999

Copy link
Copy Markdown

let alphabets = [
"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m",
"n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"
]
var password = ""
for _ in 0..<6{
var int = Int.random(in: 0..<alphabets.count)
password+=alphabets[int]

}

print(password)

@eltonbrayner

eltonbrayner commented Jun 9, 2023

Copy link
Copy Markdown
func exercise(){
  let alphabet = ["a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"]
  
  //The number of letters in alphabet equals 26
  var password = ""
  
  while(password.count < 6){
      password += alphabet[Int.random(in: 0...25)]
  }
  
  print(password)
}

ghost commented Jun 24, 2023

Copy link
Copy Markdown

bro these comments are a hoot. You got dudes on here trying to show off by doing it a far more experienced way like this isn't a class for absolute beginners lol

Right lmao

@Ven1884

Ven1884 commented Jun 30, 2023

Copy link
Copy Markdown

var alphabet = ["a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"]
alphabet.shuffle()
let password = alphabet.prefix(6).joined()
print(password)

@0ys0219

0ys0219 commented Aug 23, 2023

Copy link
Copy Markdown

let alphabet = ["a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"]

let password = { () -> String in
var str = String()
for _ in 0...5 {
str.append(alphabet.randomElement()!)
}
return str
}()

print(password)

@Danfelogar

Copy link
Copy Markdown

func exercise() {

let alphabet = ["a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"]

var randomPassword = ""
//The number of letters in alphabet equals 26
for i in 0...5 {
    randomPassword += alphabet[Int.random(in: i...alphabet.count)]
}

let password = randomPassword

print(password)

}

@GangChenGoCode

Copy link
Copy Markdown

let password = (0...5).map { _ in alphabet[Int.random(in:0...25)]}.joined()

@leandroqros

Copy link
Copy Markdown

Oi comunidade Brasileira 🇧🇷
A ideia que eu tive aqui foi criar seis variáveis e tirar um numero aleatorio de cada um na criação da variável.
Logo somar todas elas e adicionar na variável "senha"
Então imprimir a senha.

`

let alphabet = ["a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"]

//The number of letters in alphabet equals 26

//Variaveis e asignaçao de letra
var letraUm = alphabet[Int.random(in: 0...25)]
var letraDois = alphabet[Int.random(in: 0...25)]
var letraTres = alphabet[Int.random(in: 0...25)]
var letraQuatro = alphabet[Int.random(in: 0...25)]
var letraCinco = alphabet[Int.random(in: 0...25)]
var letraSeis = alphabet[Int.random(in: 0...25)]

//Soma e asignaçao de senha
var senha = letraUm + letraDois + letraTres + letraQuatro + letraCinco + letraSeis

//Imprimir senha
print(senha)

`

@marekrakus03

Copy link
Copy Markdown
let alphabet = ["a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"]


var randomCharacter1 = alphabet.randomElement()!
var randomCharacter2 = alphabet.randomElement()!
var randomCharacter3 = alphabet.randomElement()!
var randomCharacter4 = alphabet.randomElement()!
var randomCharacter5 = alphabet.randomElement()!
var randomCharacter6 = alphabet.randomElement()!

let password = randomCharacter1 + randomCharacter2 + randomCharacter3 + randomCharacter4 + randomCharacter5 + randomCharacter6

print(password)

//Went about it this way. I know it's a bit complicated but it works! While troubleshooting this I came across something called an "optional" which is a value you have to unwrap for it to be there. After searching on Stack Overflow for a while I found out you do that with a ! after the .randomElement. Have a nice day!

@astraverse-ai

Copy link
Copy Markdown

func exercise() {
let alphabet = ["a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"]
var password = ""
for _ in 0..<6 {
password += alphabet.randomElement()!
}
print(password)
}
//exercise()

@kesselly4099

Copy link
Copy Markdown

let password = alphabet.randomElement()! + alphabet.randomElement()! + alphabet[2] + alphabet[3] + alphabet[4] + alphabet[5]

// you can chose to use the randomElement of you can call single character

@FadhilMustari

Copy link
Copy Markdown

func exercise() {
let alphabet = ["a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"]

//The number of letters in alphabet equals 26
var password: String = ""
for i in 1...6{
    password += alphabet[Int.random(in: 0...25)]
}
print(password)

}

@Nurreles

Nurreles commented Feb 7, 2024

Copy link
Copy Markdown

let alphabetArray = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"]

var password = ""
for l in 0..<6 {
password += alphabetArray.randomElement() ?? ""
}

print(password)

@uncommon-design

Copy link
Copy Markdown

let alphabet = ["a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"]

var password = alphabet.randomElement()!+alphabet.randomElement()!+alphabet.randomElement()!+alphabet.randomElement()!+alphabet.randomElement()!+alphabet.randomElement()!

print(password)

@Cyrille-Dakhlia

Copy link
Copy Markdown

I really know I am late, I've all the top solutions on this page and I really don't get what does "!" mean in alphabet.rendomElemnet()! I've been looking all over internet (in my capacity) but still haven't found a reasonable solution. I hope you guys can help me on this . Thanks

Types, say String or Int, have their variant type called Optional (Optional String written in code as String?, Optional Int written in code as Int?).
A String? can either be a String or a null value (called nil in Swift) that means "nothing".

Why do we have that? Let's say you have a program where you can define nicknames for your users, you could give it the String? type as not everybody necessarily have a nickname, so the value of a nickname variable could be either a String, either nothing (nil) if no nickname would have been defined for that user.

Now let's say you have the following function that greets your users using their nickname

func greet(personNickname: String) -> String {
    let greeting = "Hello, " + personNickname + "."
    return greeting
}

It expects the nickname as a parameter being a String and not a String?. To call your function, you could not do:

print(greet(user.nickname))

since nickname is a String? and the function expects a String, you would have a compilation error.
To effectively pass a String to your function, you would use the ! symbol that says: "I know there's a value in there, trust me and act as if it was not 'Optional'." :

print(greet(user.nickname!))

Looking at the randomElement method of the Array class in the documentation, we can see that whenever we call it, it returns a value of the type Element?:

func randomElement() -> Self.Element?

Our array alphabet being an array of String elements, calling randomElement on it would return the type String?.

Since our password is of type String and not String?, it should be composed of Strings, so using the randomElement method, we should transform the String? values into String ones by using the ! symbol/operator.

@YashDjsonDookun

Copy link
Copy Markdown
let alphabet = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"];
    var password = "";
    for i in 0...5 {
        password += alphabet.randomElement() ?? "";
    }
    print(password);

@Sirio2022

Copy link
Copy Markdown

let alphabet = ["a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"]

//The number of letters in alphabet equals 26

var a = alphabet.randomElement()
var b = alphabet.randomElement()
var c = alphabet.randomElement()
var d = alphabet.randomElement()
var e = alphabet.randomElement()
var f = alphabet.randomElement()

var result = ""
result.append(a!)
result.append(b!)
result.append(c!)
result.append(d!)
result.append(e!)
result.append(f!)

let password = result

print(password)

@h40hm4ru

h40hm4ru commented May 7, 2024

Copy link
Copy Markdown
let alphabet = ["a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"]

var password = ""
for _ in 1 ... 6 {
    password += alphabet.randomElement()!
}

print(password)

@ParsaMotiallah

Copy link
Copy Markdown

let alphabet = ["a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"]

let randomElements = Array(alphabet.shuffled().prefix(6))

let password = randomElements.joined()
print (password)

@toporusan

Copy link
Copy Markdown

let alphabet = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"]
var password = ""
while password.count != 6 {
password = password + alphabet[Int.random(in: 0 ... alphabet.count - 1)]
}
print(password)

@vovahvfr

Copy link
Copy Markdown

func exercise() {
let alphabet = ["a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"];

let password = alphabet.shuffled().prefix(6).joined()

print(password);

}

@duongdqdev

duongdqdev commented Oct 27, 2024

Copy link
Copy Markdown

let alphabet = ["a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"];

let a = alphabet.randomElement() ?? "";
let b = alphabet.randomElement() ?? "";
let c = alphabet.randomElement() ?? "";
let d = alphabet.randomElement() ?? "";
let e = alphabet.randomElement() ?? "";
let f = alphabet.randomElement() ?? "";
let password = a + b + c + d + e + f;

print(password);

@5wift-Hacker

Copy link
Copy Markdown

Hi everyone! A lot of people here using more advanced techniques to solve the challenge. It's interesting to see all the ways it can be done. I'm glad I was also exactly like the official solution to show I'm on track!

@NgoniKatsidziraDev

Copy link
Copy Markdown

let alphabet = [
"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p",
"q", "r", "s", "t", "u", "v", "w", "x", "y", "z"
]

// The number of letters in alphabet equals 26

// a password of six random characters
var password: String = " "

for i in 1...6 {
password += alphabet.randomElement() ?? ""
}

print(password)

@micko4435

Copy link
Copy Markdown

func exercise() {
let alphabet = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"]

// Shuffle the alphabet and pick the first 6 letters
let shuffledAlphabet = alphabet.shuffled()
let password = shuffledAlphabet.prefix(6).joined()

print(password)

}

@pbsi-00

pbsi-00 commented Jan 27, 2025

Copy link
Copy Markdown

let alphabet = ["a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"]

//The number of letters in alphabet equals 26

let password = alphabet.randomElement()! + alphabet.randomElement()! + alphabet.randomElement()! + alphabet.randomElement()! + alphabet.randomElement()! + alphabet.randomElement()!

print(password)

The '!' on my code refers to force unwrapping optionals. Basically what optionals are saying is, "There is a value and it is [this value] or there isn't a value at all." If there wasn't a value at all, it would be 'nil'. if tried to run with randomElement alone the code would crash as will keep running and never find a solution.

Thank you for explaining this one. I am actually trying to use the alphabet.randomElement() but I was getting compiler error -- took too long to compile. Did not know you would need the '!'.

@EmmanuelLwele

Copy link
Copy Markdown

func exercise() {
let alphabet = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"]

let password = (1...6).map { _ in alphabet.randomElement()! }.joined()

print(password)

}

@HeHertZ

HeHertZ commented Aug 28, 2025

Copy link
Copy Markdown
let alphabet = ["a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"]
//The number of letters in alphabet equals 26

let password = (0..<6).compactMap { _ in
    alphabet.randomElement()}.joined()

print(password)

@elbarbaryy

Copy link
Copy Markdown

`func exercise() {

let alphabet = ["a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"]

var password = ""

for _ in 1...6{
    password += alphabet.randomElement()!
}

print(password)

}`

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