0

So I've been having a few problems with this shuffling code and posted a few questions on it, and now everything seems to be working, except the actual shuffling doesn't appear to be working.

I have been asked to rewrite this question repeatedly, first with the Swift 3 version of the shuffle code located here as well as now the Swift 4 code. I am on Swift 3.2, and I was getting warnings with the Swift 3 code. I can include them if desired, however I've added and deleted them several times already because a user keeps asking me to change my question

I've tried this:

func displayOfferCards() -> Void {
    var offerCards = allOfferCards()
    offerCards.shuffle()

    for (index, offerCard) in offerCards.enumerated() {
        let delay = Double(index) * 0.2
        offerCard.display(delay: delay)
    }
}

Here's also this for how the cards are being generated:

func allOfferCards() -> [OfferCard]{

    guard dataSource != nil else {
        return []
    }

    let numberOfCards = self.dataSource!.kolodaNumberOfCards(self)

    var offerCards = [OfferCard]()

    for i in 0..<numberOfCards {
        let offerCard = viewForCard(at: i)

        if let offerCard = offerCard {
            offerCards.append(offerCard as! OfferCard)
        }

    }

    return offerCards
}

And here's the shuffle function:

extension MutableCollection {
    /// Shuffles the contents of this collection.
    mutating func shuffle() {
        let c = count
        guard c > 1 else { return }

        for (firstUnshuffled, unshuffledCount) in zip(indices, stride(from: c, to: 1, by: -1)) {
            let d: IndexDistance = numericCast(arc4random_uniform(numericCast(unshuffledCount)))
            let i = index(firstUnshuffled, offsetBy: d)
            swapAt(firstUnshuffled, i)
        }
    }
}

An example, before and after:

print("DEBUG TOP " + offerCards[0].offer.title)
offerCards.shuffle()
print("DEBUG BOTTOM " + offerCards[0].offer.title)

Using the method removeFirst() is working:

offerCards.removeFirst()

So it is definitely something to do with the shuffle function itself I think.

And with PhilipMillis' suggestion:

DEBUG TOP Apple Music
DEBUG Swapping 0 with 0
DEBUG Swapping 1 with 2
DEBUG BOTTOM Apple Music
DEBUG TOP Apple Music
DEBUG Swapping 0 with 1
DEBUG Swapping 1 with 2
DEBUG BOTTOM Nielsen Rewards
Steven Matthews
  • 9,705
  • 45
  • 126
  • 232
  • I mean, everyone else has linked to that question as sorta the "canonical answer" for how to write a shuffle function in Swift and it is HUGE, so I figured it wasn't a good idea to include the information. I can though. And I'll include some of the debug information. – Steven Matthews Jan 17 '18 at 15:14
  • I've included the functions I used now – Steven Matthews Jan 17 '18 at 15:19
  • I posted two because I've -tried- two and neither have worked. I posted a shuffled function because I also tried the shuffled function as well above, as I have two instances of examples I've tried. None of this code has worked. – Steven Matthews Jan 17 '18 at 15:22
  • There, now I've deleted all of the Swift 4 examples, as well as the shuffled portion – Steven Matthews Jan 17 '18 at 15:26
  • I’ve tried that version too @Martin R, but deleted it from my question because rmaddy argued it was “unhelpful” – Steven Matthews Jan 17 '18 at 15:32
  • I included the lines in question, however someone posted a comment and then deleted it saying that the solution to the Swift 3 function was to just use the Swift 4 function, which had no warnings. So should I even be debugging this piece of Swift 3 code, or am I going down a rabbit hole that is wasting my time? Does Swift 3.2 use swap or swapAt? This is part of the reason I included the Swift 4 functions in my original question as well. So that someone would be able to point that out. – Steven Matthews Jan 17 '18 at 15:47
  • Yes, I literally posted all of this information earlier and YOU told me to delete it! – Steven Matthews Jan 17 '18 at 15:53
  • I described my entire process with each version of the function and you told me that was unhelpful to debug my issue. Even though it's not. – Steven Matthews Jan 17 '18 at 15:53
  • There's a difference between simply posting 4 functions with no details and focusing on the one you are actually using and clearly describing the issues you having with that one function. You are required, as the person asking a question, to post [a minimal, complete, and verifiable code example](https://stackoverflow.com/help/mcve) that clearly demonstrates your issue. That's the point I've been trying to get you too this whole time. – rmaddy Jan 17 '18 at 16:00
  • A minimal example would include both the Swift 3 and Swift 4 code from that link because it's entirely possible that neither is applicable to my specific version of Swift and I'm misunderstanding some aspect of Swift 3.2. Anyway, are you going to continue to criticize my question writing skills, or are you going to help me? You may feel that the question is better than it was before, but I feel it's including substantially less important information to any potential reader. – Steven Matthews Jan 17 '18 at 16:05
  • How does it "not work"? If you print `offerCards` before and after the call to `shuffle()`, is there a difference or not? – Martin R Jan 17 '18 at 16:13
  • There is no difference. It always starts on the same card first. – Steven Matthews Jan 17 '18 at 16:15
  • I'll print out the offerCard examples to show – Steven Matthews Jan 17 '18 at 16:17
  • @AndrewAlexander Isn't this the third question you've asked about this? Why do you keep opening new questions? – Alexander Jan 17 '18 at 17:22
  • @AndrewAlexander You had commented about an issue with your code, I asked you to elaborate, and you left me hanging: https://chat.stackoverflow.com/rooms/163277/discussion-between-alexander-and-andrew-alexander – Alexander Jan 17 '18 at 17:23
  • That part was solved - I made an error when understanding an example someone else gave. This is a separate problem, just related to the previous question. – Steven Matthews Jan 17 '18 at 17:30
  • @MartinR - included an example. The before and after shuffle first card is the same one. – Steven Matthews Jan 17 '18 at 18:19
  • I just tried your `shuffle` function in a playground using an array of strings as input. It worked reasonably well. How many items are in your array? Is it only the first that ends up in the same place? (You could put something like `print("Swapping \(firstUnshuffled) with \(i)")` inside the swap loop to trace what's happening.) – Phillip Mills Jan 17 '18 at 20:53
  • Probably about 20-30 items in the array. The first two are currently priority offers and will be placed first always. This shuffle should deal with that though (as we want the priority offers to show up as priority only under the first pass through). Unfortunately the shuffle doesn't appear to be working ever. – Steven Matthews Jan 17 '18 at 21:06
  • @PhillipMills DEBUG TOP Apple Music DEBUG Swapping 0 with 0 DEBUG Swapping 1 with 2 DEBUG BOTTOM Apple Music DEBUG TOP Apple Music DEBUG Swapping 0 with 1 DEBUG Swapping 1 with 2 DEBUG BOTTOM Nielsen Rewards – Steven Matthews Jan 17 '18 at 21:21
  • Your debug messages suggest there are only 3 items in the collection. (And your second set of printouts shows the first item being changed.) – Phillip Mills Jan 18 '18 at 12:59

0 Answers0