Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Making four separate variables

Tags:

ios

swift

xcode6

I am trying to make four separate variables that are all different so that a and b are different, a and c are different, b and c are different and so on so forth. Here are the four variables.

var a = Int(arc4random_uniform(5))
var b = Int(arc4random_uniform(5))
var c = Int(arc4random_uniform(5))
var d = Int(arc4random_uniform(5))
like image 761
needshelp Avatar asked Jan 20 '26 15:01

needshelp


2 Answers

Even though the question has been already answered, here is an alternative solution:

var set = Set<UInt32>()

while set.count < 4 {
    set.insert(arc4random_uniform(5))
}
like image 66
MirekE Avatar answered Jan 23 '26 05:01

MirekE


let a = Int(arc4random_uniform(5))
var b = Int(arc4random_uniform(5))
while b == a {
    b = Int(arc4random_uniform(5))
}
var c = Int(arc4random_uniform(5))
while c == a || c == b {
    c = Int(arc4random_uniform(5))
}
var d = Int(arc4random_uniform(5))
while d == c || d == b || d == a {
    d = Int(arc4random_uniform(5))
}
println(a)  // 2
println(b)  // 0
println(c)  // 4
println(d)  // 3
like image 35
Leo Dabus Avatar answered Jan 23 '26 05:01

Leo Dabus



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!