how to randomize an array of strings?

0

I have an array and I want to randomize this array so that every time I start the app the elements are in a different position, how can I do this? I have found different extensions on the internet, and some work, but when doing it random, they repeat some pieces and they do not work or they do not work because I get this error.

  

Can not use mutating member on immutable value of type '[String]'

I have tried to solve this error, but what I find I can not understand why the error or how to solve it, I am a beginner in swift, I'm just working with Spritekit, I thought the error was because the array is in a struct, and I changed it to a class but it did not work, I'm using swift 4.1 I hope somebody could guide me on how to solve this problem I thank you from now:)

EDIT I have found the error of repeating elements, that array and properties I have in another class / struct and I need to accommodate the elements in a certain way once I put them in, I add them to the scene with a for and that for, I believe to call the class 25 times so that's why I repeat the elements, I hope I could have explained and hopefully now if you can help me :(

 for container in 0...24{

            self.addChild(containerSprite[container].block)

        }
    
asked by Heber Solis 23.08.2018 в 05:19
source

3 answers

2

the solution would be:

  var names = ["name1", "name2", "name3", "name4"];

  for index in names.indices {
   let number = arc4random_uniform(UInt32(names.count))
   names.swapAt(index.hashValue, Int(number))
  }

For iOS 12, or xcode 10, there will be a new function called shuffle (), which will randomize the arrays, I'll leave the link. link

That is, it would only be

var names = ["name1", "name2", "name3", "name4"];
names.shuffle()
    
answered by 23.08.2018 в 20:35
0

The solution I propose is the following:

  • You create an array that is initially empty.
  • You get the number of elements that the array has with all the Strings (array.count)
  • You make a loop until that number of elements is 0, and in each iteration of the loop:

    • You generate a random number between 0 and the number of elements -1
    • You remove the elements from the array with the Strings, insert it in the empty array and eliminate it from the initial one.
    • You subtract one from the number of elements that are in the initial array.
  • Sorry I can not send you the code, anyway, the solution is what matters:)

        
    answered by 23.08.2018 в 17:12
    0

    One option can be:

    func randomArray() {
        var randomValues: [String] = [String]()
        var copyContainers = containers
    
        for _ in 0..<containers.count {
            let random = copyContainers.randomElement()
            randomValues.append(random)
            copyContainers = copyContainers.filter{$0 != random}
        }
    
        print(randomValues)
    }
    
    extension Array {
        func randomElement() -> Element  {
            return self[Int(arc4random_uniform(UInt32(self.count)))]
        }
    }
    
        
    answered by 23.08.2018 в 20:23