Geniouse Geniouse - 6 months ago 10
Swift Question

Swift: How do you set up a function with no parameters to return a random array of numbers?

I'm trying to return at least 10 random numbers but i am only able to return 1 random number. My code is:

func randNumbers () -> [Int]{
var numbers: [Int] = []
numbers.append(Int(arc4random_uniform(50) + 1))
return numbers
}
randNumbers()


Any help is really appreciated :)

Answer

You need to append at least 10 numbers into the array, so you can use a loop for this.

func randNumbers () -> [Int]{
    var numbers: [Int] = []
    for _ in 1...10 {
        numbers.append(Int(arc4random_uniform(50) + 1))
    }
    return numbers
}