Reputation: 17
So I have to randomize an x position. But for some reason the middle is 0 and therefore I need to do negative max x and positive max x. It's a little hard to explain so here is the code:
let height = UInt32(self.view!.frame.height)
let nHeight = ((height - height) - height)
let randomXPosition = Int(arc4random_uniform(UInt32(nHeight)) + height)
This doesn't give me any errors but when I run it crashes. I need a way to randomize the value for negative max X and positive max X so that the object is randomized in the screen.
Upvotes: 0
Views: 47
Reputation: 5521
If you need to generate a random number between -x and x then just create a random number between 0 and 2x and subtract x. arc4random_uniform(x * 2) - x
.
Upvotes: 3