If all you want to do is to show a random number every time the button is clicked, here is some code that can help.
But first, the line you use to create the position is wrong. Do this instead:
int position = (arc4random() % 5) + 1; // Creates a random number between 1 and 5.
int position = (arc4random() % 5) - 1; // Creates a random number between -1 and 3.
Second, I'd suggest to use an NSArray
or NSMutableArray
to hold your data.
I assume that you have a method that is called when you press a button. Inside that method you can simply put something like this:
int size = 5; // You might want to get the size of your array dynamically, with something like [usedNumbers count];
int position = (arc4random() % size) + 1; // Generates a number between 1-5.
NSNumber *randomNumber = [usedNumbers objectAtIndex:position]; // Here is your random number from the array.
So.. If you add the array as an instance variable to your class, your header file would look something like this:
@interface MyViewController : UIViewController
@property (nonatomic, retain) NSMutableArray *usedNumbers;
- (IBAction)buttonWasClicked:(id)sender; // Remember to connect it to your button in Interface Builder.
@end
And your implementation file:
@implementation MyViewController
@synthesize usedNumbers;
- (void)viewDidLoad {
// Initialize your array and add the numbers.
usedNumbers = [[NSMutableArray alloc] init];
[usedNumbers addObject:[NSNumber numberWithInt:4]];
[usedNumbers addObject:[NSNumber numberWithInt:13]];
// Add as many numbers as you'd like.
}
- (IBAction)buttonWasClicked:(id)sender {
int size = [usedNumbers count];
int position = (arc4random() % size); // Generates a number between 0 and 4, instead of 1-5.
// This is because the indexes in the array starts at 0. So when you have 5 elements, the highest index is 4.
NSNumber *randomNumber = [usedNumbers objectAtIndex:position]; // The number chosen by the random index (position).
NSLog(@"Random position: %d", position);
NSLog(@"Number at that position: %@", randomNumber);
}
@end
If you do it like this, a random number will be chosen from the array every time the button is clicked.
Also, remember to release
all your objects if you don't have ARC enabled.
PS: There are several other questions here on SO covering this subject. Remember to search before asking.
Update:
To make sure every number is used only once, you can remove it from your array when it is chosen. So the buttonWasClicked:
method will be something like this:
- (IBAction)buttonWasClicked:(id)sender {
int size = [usedNumbers count];
if (size > 0) {
int position = (arc4random() % size);
NSNumber *randomNumber = [usedNumbers objectAtIndex:position];
// Do something with your number.
// Finally, remove it from the array:
[usedNumbers removeObjectAtIndex:position];
} else {
// The array is empty.
}
}