随机生成器 Objective-C

问题描述:

我已经声明了一个从 1 到 5 的静态整数数组,现在想要通过每次按下按钮随机调用每个数字.我不希望同一个号码被拨打两次.

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.

如果您只想在每次单击按钮时显示一个随机数,这里有一些代码可以提供帮助.

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. 

其次,我建议使用 NSArrayNSMutableArray 来保存您的数据.

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

还有你的实现文件:

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

此外,如果您没有启用 ARC,请记住 release 所有对象.

Also, remember to release all your objects if you don't have ARC enabled.

PS:这里还有其他几个关于这个主题的问题.提问前记得先搜索.

PS: There are several other questions here on SO covering this subject. Remember to search before asking.

更新:

为确保每个数字只使用一次,您可以在选择它时将其从数组中删除.所以 buttonWasClicked: 方法将是这样的:

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