views:

559

answers:

4

I have used rand(). But it gives a specific value even after I restart application.

I have implemented following in my application.

- (void)viewDidLoad {
[super viewDidLoad];
int x,y;

x=random() % 480; y=random() % 300;

lblT.center=CGPointMake(x,y); // my label lblT
}

Try to implement in your application, and launch application. After restarting application, you will find that label will be on specific value.

+6  A: 

Use the following line before random()

srand(time(NULL));

There you go.

gcamp
+2  A: 

Random number generators in software will actually give PSEUDORANDOM sequences of values.

Unless you seed the random number generator with a value from a truly random event, you will always get the same sequence each time you use the software.

I don't know about your software, but it doesn't look like you're seeding the random number generator, Gcamp is probably on the right track.

pavium
+6  A: 

arc4random() is a built-in function that does not require seeding (and so therefore does not produce predictable sequences that can be generated by using a seed), and it conveniently returns integers. I never use anything else.

Dave DeLong
+1  A: 

Since you are using random() and not rand() you should seed the random generator with this bit of code...

srandomdev();

probablyCorey