xcode 用于彩票选择器错误的 Objective-C 随机数生成器
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6400292/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Objective-C random number generator for lottery picker bug
提问by Mirko
I have put together some code in objective-c to help me create a random number generator for a lottery picker program. But i seem to have a problem where it sometimes comes up with the same number in the 5 various columns even after there is a statement to check for it first. When its compiled and run it will create the random numbers and most of the times they are different but it will at different times come up with the same number in two rows when it shouldn't.
我在 Objective-c 中组合了一些代码来帮助我为彩票选择器程序创建一个随机数生成器。但我似乎有一个问题,即使在有先检查它的语句之后,它有时也会在 5 个不同的列中出现相同的数字。当它编译并运行时,它会创建随机数,而且大多数时候它们是不同的,但它会在不同的时间在两行中出现相同的数字,而不应该这样做。
I have tried usiong both rand() and also arc4random() and both give me the same issue.
我已经尝试使用 rand() 和 arc4random() 并且都给我同样的问题。
int rNumber1 = arc4random() % 55 + 1; // 55 because it chooses from 0 to 55 then we add one, max 56
int rNumber2 = arc4random() % 55 + 1; // 55 because it chooses from 0 to 55 then we add one, max 56
int rNumber3 = arc4random() % 55 + 1; // 55 because it chooses from 0 to 55 then we add one, max 56
int rNumber4 = arc4random() % 55 + 1; // 55 because it chooses from 0 to 55 then we add one, max 56
int rNumber5 = arc4random() % 55 + 1; // 55 because it chooses from 0 to 55 then we add one, max 56
// First number box result
textView01.text = [[NSString alloc] initWithFormat:@"%d", rNumber1];
// Second number box result not equal to first box number
if (rNumber2 != rNumber1) {
textView02.text = [[NSString alloc] initWithFormat:@"%d", rNumber2];
} ;
// Third number box result not equal to first or second box number
if (rNumber3 != rNumber1 || rNumber2){
textView03.text = [[NSString alloc] initWithFormat:@"%d", rNumber3];
} ;
// Fourth box number result not equal to first, second or third number box number
if(rNumber4 != rNumber1 || rNumber2 || rNumber3){
textView04.text = [[NSString alloc] initWithFormat:@"%d", rNumber4];
} ;
// Fifth box number result not equal to first, second, third or fourth number box number
if (rNumber5 != rNumber1 || rNumber2 || rNumber3 || rNumber4){
textView05.text = [[NSString alloc] initWithFormat:@"%d", rNumber5];
} ;
// Sixth box number independant of other boxes, up to 46 max number
int rNumber6 = rand() % 45 + 1; // 45 because it chooses from 0 to 45 then we add one, max 46
textView06.text = [[NSString alloc] initWithFormat:@"%d", rNumber6];
回答by David Gelhar
This statement:
这个说法:
if (rNumber3 != rNumber1 || rNumber2){
does not do what you think it does.
不会做你认为它会做的事情。
"||" has lower precedence than "!=", so what you have is equivalent to:
“||” 优先级低于“!=”,所以你所拥有的相当于:
if ((rNumber3 != rNumber1) || (rNumber2 != 0)) {
The "!=" operator in C (and Objective-C) always applies to just 2 arguments; to compare one number against a bunch of others, you need to explicitly repeat the first number each time:
C(和 Objective-C)中的“!=”操作符总是只适用于 2 个参数;要将一个数字与一堆其他数字进行比较,您需要每次明确重复第一个数字:
if (rNumber3 != rNumber1 || rNumber3 != rNumber2)
ps. the following comment is incorrect:
附:以下评论不正确:
int rNumber6 = rand() % 45 + 1; // 45 because it chooses from 0 to 45 then we add one, max 46
rand() % 45
returns a value from 0 to 44, not 0 to 45
rand() % 45
返回 0 到 44 之间的值,而不是 0 到 45
回答by Deepak Danduprolu
Use NSMutableSet
to deal with identical numbers,
使用NSMutableSet
处理相同的数字,
NSMutableSet * numberSet = [NSMutableSet setWithCapacity:5];
while ([numberSet count] < 5 ) {
NSNumber * randomNumber = [NSNumber numberWithInt:(arc4random() % 56 + 1)];
[numberSet addObject:randomNumber];
}
NSArray * numbers = [numberSet allObjects];
textView01.text = [NSString stringWithFormat:@"%@", [numbers objectAtIndex:0]];
textView02.text = [NSString stringWithFormat:@"%@", [numbers objectAtIndex:1]];
textView03.text = [NSString stringWithFormat:@"%@", [numbers objectAtIndex:2]];
textView04.text = [NSString stringWithFormat:@"%@", [numbers objectAtIndex:3]];
textView05.text = [NSString stringWithFormat:@"%@", [numbers objectAtIndex:4]];
And btw, arc4random() % 55 + 1
will give you numbers between 1 and 55 including both of them but not 56.
顺便说一句,arc4random() % 55 + 1
会给你 1 到 55 之间的数字,包括它们,但不包括 56。