1

I have an array of Int and I would like to pick a random Int from my array, with a specific conditions to fill a 2D grid. I tried the following code it ran fine for a while but in some cases it's running with an infinite while loop.

    for row in 0..<NumRows {
        for column in 0..<NumColumns {

                var gemType:GemType

                if gemArray.count == 1 {

                   gemType = GemType(rawValue: gemArray[0])!

                } else {

                    do {

                        index = Int(arc4random_uniform(UInt32(gemArray.count)))

                        gemType = GemType(rawValue: gemArray[index])!

                    } while (column >= 1 &&
                        gems[column - 1, row]?.gemType == gemType)
                        || (row >= 1 &&
                            gems[column, row - 1]?.gemType == gemType)

                    if gemArray.count > 0 {

                        gemArray.removeAtIndex(index)
                    }
                }

                let gem = Gem(column: column, row: row, gemType: gemType)
                gems[column, row] = gem

                // 4
                set.insert(gem)
            }
        }
    }
    return set

Any help?

Mariam
  • 683
  • 1
  • 11
  • 27
  • possible duplicate of [Pick a random element from an array](http://stackoverflow.com/questions/24003191/pick-a-random-element-from-an-array) – sangony May 21 '15 at 13:46
  • @sangony I already checked it :( it's different than mine because I want to pick all my array elements in a random order not just one element. – Mariam May 21 '15 at 13:49
  • you can generate a random number with your specified range like this: http://swiftstub.com/925709499/ – Dan Beaulieu May 21 '15 at 13:53
  • I have a feeling that it's your loops and conditions above that are having the problem. Have you tried doing away all the loops and try out a simple version? – Wraithseeker May 21 '15 at 13:54
  • @DanBeaulieu Not a range I need a specific numbers I put them in the array. – Mariam May 21 '15 at 13:59
  • @Wraithseekerr I totally agree with you. Yes I tried and it works perfectly, but I need the conditions Any help? How can I fix it? – Mariam May 21 '15 at 14:00
  • @Mariam right, so you select the numbers that you do want. Then use a random number to select one of your numbers from the array? Maybe im not understanding your problem – Dan Beaulieu May 21 '15 at 14:01
  • @DanBeaulieu Yes, And I have to pick them randomly with a specific conditions which are in the while loop and I think they cause the problem. – Mariam May 21 '15 at 14:03
  • while (column >= 1 && gems[column - 1, row]?.gemType == gemType) || (row >= 1 && gems[column, row - 1]?.gemType == gemType) I think therre's a bug with that line, might want to check it out. I looked through the other code and they look fine to me. – Wraithseeker May 21 '15 at 14:09
  • @Wraithseekerr I agree with you. Do you thing I have to use another way better than do while? Any suggestion? I tried switch with where it works but it breaks the game logic. – Mariam May 21 '15 at 14:16
  • If you want to pick all your array elements in a random order, you want a [shuffle](http://en.wikipedia.org/wiki/Fisher–Yates_shuffle) instead of a randomized pick at each step. See http://stackoverflow.com/questions/24026510/how-do-i-shuffle-an-array-in-swift – pjs May 21 '15 at 14:17
  • @pjs Yes I was thinking to use shuffle instead of randomized but I need the condition in the while loop, so do you think it's better to use a shuffle then I add my conditions in the shuffle method? – Mariam May 21 '15 at 14:20
  • @Mariam I'd try building subset lists that meet your conditions, and shuffle the appropriate subset. But then I don't know your exact problem, so there may be constraints that would preclude that. – pjs May 21 '15 at 15:48

1 Answers1

1

A nice way of selecting all the numbers in a random way could be as follows. (Note: this is only a pseudocode.)

Suppose array is of size [1...n]
1. k=n
2. Choose a random number between 1 to k. Let it be x.
3. Swap a[k] with a[x]. Your chosen random number is a[x].
4. k=k-1.
5. Loop until k=1.
Anindya Dutta
  • 1,972
  • 2
  • 18
  • 33
  • thanks but this is like a shuffle I need a shuffle with a specific conditions which I wrote in the while loop and these conditions cause my problem. – Mariam May 22 '15 at 18:38