2011-11-25 191 views
0

我已经声明了一个从1到5的整数的静态数组,现在想每次按下按钮随机地调用每个数字。我不想让相同的号码被呼叫两次。随机生成器Objective-C

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; 

这是我迄今为止所做的。我的逻辑在某个地方有一个漏洞,我可以做一些帮助。我认为这与切换器有关,它试图保留一个数字而另一个被删除。

+0

可能重复http://stackoverflow.com/questions/791232/canonical-way-to-randomize -an-nsarray-in-objective-c),[非重复的随机数字](http://stackoverflow.com/questions/1617630/) – outis

+0

另请参见[什么是洗牌NSMutableArray的最佳方式?](http:/ /stackoverflow.com/questions/56648/) – outis

+0

[唯一随机数在整数数组中]的可能重复(http://stackoverflow.com/questions/1608181/)或[非重复随机数](http:// stackoverflow.com/questions/1617630/) –

回答

2

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

但是,首先,您用于创建位置的行是错误的。做到这一点,而不是:

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来保存数据。

我假设你有一个当你按下按钮时被调用的方法。内部的方法,你可以简单地把这样的事情:

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. 

所以..如果添加了数组作为实例变量到类中,头文件将是这个样子:

@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 

如果你这样做,每次单击按钮时都会从数组中选择一个随机数。如果您没有启用ARC,请记住release您的所有对象。

PS:这里还有其他几个关于这个主题的问题。请记住在提问前进行搜索。

更新:

要确保每个数字只能使用一次,你可以从你的阵列在选择时,也将其删除。所以buttonWasClicked:方法是这样的:

- (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. 
    } 
} 
的[规范的方法,以随机目标C一个NSArray(
+0

我很感激帮助,但这种方法肯定不会阻止一个数字重复多次?所选号码必须每次都是唯一的,直到它们全部通过。 – Bushes

+0

无论如何,我已经设法解决它,但欢呼的帮助。 – Bushes

+0

你是对的,我的坏。我没有读好你的答案。你可以简单地删除'buttonWasClicked:'中选择的数字,用下面这行代码:'[usedNumbers removeObjectAtIndex:position]'。我会更新我的答案。 – matsr