1

How can I make a random CGPointMake, say i created an outlet of a uiimage and called it image, image.center = CGPointMake (//random, //random) everytime second (NSTimer)

user840797
  • 36
  • 5

3 Answers3

2

I'd use arc4random() in stdlib.h. This utilizes a much more superior algorithm than rand(). Look up this function in the man pages. You will then want to mod the generated values by the maximum coordinates that you want for both axes.

int x = arc4random() % (int) self.view.frame.size.width;
int y = arc4random() % (int) self.view.frame.size.height;

image.center = CGPointMake(x, y);
csano
  • 13,266
  • 2
  • 28
  • 45
1

you can use rand() in stdlib.h

something like:

#include <stdlib.h>
#include <time.h>

srand ( time(NULL) );
int max = 400         // or whatever
int myRandomNumber1 = rand() % max
int myRandomNumber2 = rand() % max

image.center = CGPointMake(myRandomNumber1, myRandomNumber2);
meggar
  • 1,219
  • 1
  • 10
  • 18
0

Use arc4random(), you don't even need to seed it.

image.center = CGPointMake(arc4random() % yourView.frame.size.width, arc4random() % yourView.frame.size.height);
Luke
  • 7,110
  • 6
  • 45
  • 74