在iOS中生成范围内的随机数?

我试图得到一个随机数发生器在iPhone上工作。 有两个文本字段标签和一个button。 在一个文本框中input最小数量,然后在下一个文本框中input最大数量 点击button将显示UILabel中的随机数。 我曾经这样做过一次,也无法想象今天的生活。 任何代码或我可以访问的地方find这个将是太棒了。

谢谢

NSString *min = myMinTextField.text; //Get the current text from your minimum and maximum textfields. NSString *max = myMaxTextField.text; int randNum = rand() % ([max intValue] - [min intValue]) + [min intValue]; //create the random number. NSString *num = [NSString stringWithFormat:@"%d", randNum]; //Make the number into a string. [myLabel setText:num]; // Give your label the value of the string that contains the number. 

更新:

看起来,使用arc4random可能更好,而不是rand你可以在这里看到更多的信息。 只需用arc4randomreplace所有的rand

 #include <stdlib.h> int randomNumber = min + rand() % (max-min);