-2

I'm extremely new to Swift and programming in general so please be patient with me while I'm trying to get the hang of this.

I've been following a beginners course in Swift from Treehouse and managed to develop a simple app that generates random quotes. So far so good. Now before moving on to more advanced lessons I figured I'd try to update the existing app just to make sure I get some solid practice before moving on.

So here's my question: I've managed to generate a random number through the GameKit framework, but the problem is that sometimes quotes appear consecutively. How can I avoid this from happening?

Here's my code:

import GameKit

struct FactProvider {
    let facts = [
        "Ants stretch when they wake up in the morning.",
        "Ostriches can run faster than horses.",
        "Olympic gold medals are actually made mostly of silver.",
        "You are born with 300 bones; by the time you are an adult you will have 206.",
        "It takes about 8 minutes for light from the Sun to reach Earth.",
        "Some bamboo plants can grow almost a meter in just one day.",
        "The state of Florida is bigger than England.",
        "Some penguins can leap 2-3 meters out of the water.",
        "On average, it takes 66 days to form a new habit.",
        "Mammoths still walked the Earth when the Great Pyramid was being built."
    ]

    func randomFact() -> String {
        let randomNumber = GKRandomSource.sharedRandom().nextInt(upperBound: facts.count)
        return facts[randomNumber]
    }
}
Machavity
  • 30,841
  • 27
  • 92
  • 100
imalexdae
  • 3
  • 2
  • Your set is only 10 elements wide. So you should expect some repetition to occur eventually – Machavity May 09 '17 at 13:25
  • 1
    Please have a look at the "Related" questions, such as http://stackoverflow.com/questions/27541145/how-to-generate-a-random-number-in-swift-without-repeating-the-previous-random-n and http://stackoverflow.com/questions/26457632/how-to-generate-random-numbers-without-repetition-in-swift. – And of course http://stackoverflow.com/questions/24026510/how-do-i-shuffle-an-array-in-swift – Martin R May 09 '17 at 13:26
  • @Machavity that's irrelevant. I needed them to shuffle and not show the same quote twice in a row. – imalexdae May 10 '17 at 12:34
  • @MartinR thank you for that. I've already checked the "Related" questions and all 3 of your links do not help me since they are using "arc4random_uniform" to solve the problem. I've specifically mentioned I needed to use the GameKit framework. – imalexdae May 10 '17 at 12:36
  • Well, they do show the *idea.* In fact, this answer http://stackoverflow.com/a/27541269/1187415 to the first link uses the same approach as the the answer below. – Martin R May 10 '17 at 12:40
  • @MartinR Not really. I will stress this again. I'm trying to avoid using arc4random, which is an Objective C function. I was only interested in a solution that involved GKRandomSource. https://developer.apple.com/reference/gameplaykit/gkrandomsource – imalexdae May 10 '17 at 12:49
  • The *idea* is to store the last chosen number in a variable, and repeat creating random numbers until one is different from the stored number. That approach works with arc4random or GKRandomSource or any other PRNG. You can take the code from http://stackoverflow.com/a/27541269/1187415 and replace `arc4random_uniform(10)` by GKRandomSource... or whatever. – Btw, arc4random is *not* an Objective-C function. – Martin R May 10 '17 at 12:52
  • @MartinR I see, but maybe you haven't completely read my original question. I've mentioned that I'm a noob at programming and I would like everyone to chill and bare with me. Your link would not have helped me at all giving the fact that I'm still trying grasp the logic of it all. Thank you for your comments and hopefully next time you'll actually answer my questions and be of help since your more experienced than I am. :) – imalexdae May 10 '17 at 12:59
  • Note that GameplayKit has a dedicated class to generate non-repeating random numbers, see for example http://stackoverflow.com/a/34623444/1187415 – Martin R May 10 '17 at 13:00

1 Answers1

1

You can store last random number or last fact in a variable and check it in your randomFact function. Like this:

var lastRandomNumber = -1

func randomFact() -> String {
    let randomNumber = GKRandomSource.sharedRandom().nextInt(upperBound: facts.count)

    if randomNumber == lastRandomNumber {
        return randomFact()
    } else {
        lastRandomNumber = randomNumber
        return facts[randomNumber]
    }
}
dilaver
  • 674
  • 3
  • 17