Random number from an array without repeating the same number twice in a row?

前端 未结 4 1462
慢半拍i
慢半拍i 2020-12-22 10:32

I am making a game using Swift and SpriteKit where i move an object to random locations based on an array.

The array that is made up of CGPoints:

let         


        
4条回答
  •  余生分开走
    2020-12-22 10:49

    The code below doesn't random the same number.

       var currentNo: UInt32 = 0
    
        func randomNumber(maximum: UInt32) -> Int {
    
            var randomNumber: UInt32
    
            do {
                randomNumber = (arc4random_uniform(maximum))
            }while currentNo == randomNumber
    
            currentNo = randomNumber
    
            return Int(randomNumber)
        }
    

提交回复
热议问题