Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

generating random values in iPhone

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.

like image 354
Sagar Kothari Avatar asked Feb 25 '26 00:02

Sagar Kothari


2 Answers

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.

like image 137
Dave DeLong Avatar answered Feb 27 '26 20:02

Dave DeLong


Use the following line before random()

srand(time(NULL));

like image 34
gcamp Avatar answered Feb 27 '26 20:02

gcamp