Random generator Objective-C

て烟熏妆下的殇ゞ 提交于 2019-12-13 06:21:59

问题


I've declared a static array of integer from 1 to 5 and now want to call randomly each number with each press of a button. I don't want the same number to be called twice.

int randomNumber;
static int size = 5;

int position = arc4random() % size - 1;
randomNumber = usedNumbers[position];
int switcher = usedNumbers[size]; // wrong
usedNumbers[position] = usedNumbers[size];
size--;
usedNumbers[position] = switcher;

Here's what I've done so far. There's a hole in my logic somewhere and I could do with some help. I think it's something to do with the switcher which is trying to hold a number whilst another is being deleted.


回答1:


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.
    }
}


来源:https://stackoverflow.com/questions/8275009/random-generator-objective-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!