You should use arc4random(), not random(). arc4radom gives much better random numbers, and it's self-seeding, so you don't get the same sequence every time you run your program.
Second, you need to use the modulo operator ("%") to get a random number in a given range. Try this:
Code:
CGFloat x = (CGFloat) arc4random() % self.view.bounds.size.width;
CGFloat y = (CGFloat) random() % self.view.bounds.size.height;
Note that if you let the ball's center move all the way from 0 to the height/width of your bounds, the ball will be 1/2 off screen at the max/min value. You should really keep it from going below ball.bounds.size.width/2, ball.bounds.size.height/2, and don't let it go above self.view.bounds.size.width - ball.bounds.size.width/2, self.view.bounds.size.height - ball.bounds.size.height/2;
Check out this password generator app that shows various techniques including using a data container singleton object to share data between objects in your project.
You should use arc4random(), not random(). arc4radom gives much better random numbers, and it's self-seeding, so you don't get the same sequence every time you run your program...........
thank you ,, but the compiler gives me this errors :
Check out this password generator app that shows various techniques including using a data container singleton object to share data between objects in your project.
I think problem its because of % ! , when I change % to / or * works fine ! but doesn't move great .
No, it's not my code, it's yours. I'm trying to help you out. You need to experiment and figure this out for yourself. I'm trying to work on my own project and help out a little on the forum too.
You might need an extra pair of parenthesis around the type cast, like this:
Code:
CGFloat x = (CGFloat) arc4random() % ((int) self.view.bounds.size.width);
CGFloat y = (CGFloat) random() % ((int) self.view.bounds.size.height);
Check out this password generator app that shows various techniques including using a data container singleton object to share data between objects in your project.
Check out this password generator app that shows various techniques including using a data container singleton object to share data between objects in your project.