Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

iphonedevelop18

macrumors member
Original poster
Feb 26, 2009
50
0
First of all I am making a tic tac toe game.

1. how can a set an int to a random number, ie.
Code:
difficulty = 5;
choice = (random() % difficulty) + 1;
if (choice != 2){
}
it will stay the same throughout as i tried to set difficulty to 100 and it would stay the same.

2. How can I delay the reaction of the computer.

Thanks!
 

mccannmarc

macrumors 6502
Aug 15, 2008
270
0
Manchester, UK
I will answer your first question but I don't quite get what you mean by the second one

The reason your random number will be the same is because you are not seeding the random number off of anything. if you put the following line before your code for generating the random number you will find it will start working. Try this:-

Code:
difficulty = 5;
srandom(time(NULL));
choice = (random() % difficulty) + 1;
if (choice != 2){
}
 

iphonedevelop18

macrumors member
Original poster
Feb 26, 2009
50
0
mccannmarc

Yea I already seeded it in the app delegate under didfinish launching or whatever. So I don't think I have to seed it again do I?
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.