Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

iOS

Need help with creating a func! I'm stuck

ok so I've created the following func.. it randomly selects 6 numbers out of 45 like a lotto draw. but now what i want to do is repeat this process until it draws out the 6 numbers i have chosen in no particular order. simulating how many lotto draws it will take for my numbers to come out, the odds are 1 in 8,145,060. I'm stuck on how to loop my for in loop thanks

 func lotto() -> [Int] {

    var result: [Int] = []

    for eachNumber in 1...6 {

        result.append(Int(arc4random_uniform(45)))

    }
    return result
}

lotto()

in swift code :)

2 Answers

Hey Roland,

I had a crack at doing something like what you asked. Warning that this may cause an infinite loop, which is why I have it only looping until a max number. I have added keepLooping where you can comment out the line below if you want to tempt fate.

My idea is just to get the random 6 numbers, sort them, compare to your sorted numbers and if the same, stop the loop.

func lotto() -> [Int] {
    var result: [Int] = []
    for _ in 1...6 {
        result.append(Int(arc4random_uniform(45)))
    }
    return result
}

func bubbleSort(numbers: [Int]) -> [Int] {
    var listOfNumbers: [Int] = numbers
    let nElements = listOfNumbers.count
    var didSwap = true
    while didSwap {
        didSwap = false

        for i in 0..<nElements - 1 {
            if listOfNumbers[i] < listOfNumbers[i+1] {
                let tmp = listOfNumbers[i]
                listOfNumbers[i] = listOfNumbers[i+1]
                listOfNumbers[i+1] = tmp
                didSwap = true
            }
        }
    }
    return listOfNumbers
}

var myList: [Int] = [41, 35, 18, 15, 7, 2]

var max = 10
var i = 0
var keepLooping = true

//while keepLooping {
while i < max {
    var randomNumbers: [Int] = bubbleSort(lotto())
    i += 1
    if myList == randomNumbers {
        print("Found after \(i) attempts")
        keepLooping = false
    }
}

Im only starting to learn swift code so I'm a novice. trying to figure out what things do is hard sometimes..can you tell me what the bubbleSort func does? i just did this and it works ok i think..

func lotto () -> Set<Int> {

var numbersDrawn: Set<Int> = []

for _ in 1...6 {

    numbersDrawn.insert(Int(arc4random_uniform(45)))
}

return numbersDrawn

}

var myList: Set<Int> = [6,10,21,28,29,35]

var max = 10000 var i = 0 var keepLooping = true

//while keepLooping { while i < max { var randomNumbers: Set<Int> = lotto() i += 1 if myList == randomNumbers { print("(randomNumbers) Found after (i) attempts") keepLooping = false } else { print ("(randomNumbers)") } }

WIth the Bubble sort code, the theory of bubble sort is that the highest number 'floats' to the top, like a bubble I guess. It checks the current item against the next and swaps if smaller, then repeats. It's a really slow process, so changing to a 'set' should improve speeds significantly if you don't need to sort. :)

yes that works. Thank you so much. only thing i changed was the instead of using ordered arrays i just changed them to to a Set as they don't need to be in an exact order.

Ok, cool. Never heard of 'Set', thats a pretty cool thing, quicker than arrays and can only hold unique elements.

Given that, your random list may not contain 6 numbers, as if it pulls the same number twice, it will only be stored once. Unique numbers are what you want though if writing a lotto app. maybe change your code to repeat until the set contains 6 numbers:

func lotto () -> Set<Int> {
  var numbersDrawn: Set<Int> = []
  var i = 0  
  while numbersDrawn.count < 6 {
    numbersDrawn.insert(Int(arc4random_uniform(45)))
  }
  return numbersDrawn
}