开发者

Objective-C random number generator for lottery picker bug

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.

I have tried usiong both rand() and also arc4random() and both give me the same issue.

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 rNum开发者_运维技巧ber6 = rand() % 45 + 1;  // 45 because it chooses from 0 to 45 then we add one, max 46
textView06.text = [[NSString alloc] initWithFormat:@"%d",  rNumber6];


Use NSMutableSet to deal with identical numbers,

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.


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:

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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜