3

I am making Quiz app in which i want to generate random numbers without repetition.
I have searched many things and got the idea but i think i am doing something wrong that's why not getting correct output.

Here is the code that i used.

-(int)generater{

NSMutableArray *temp;
srand([[NSDate date] timeIntervalSince1970]);
r = 1 + (arc4random() % 11);

if ([temp count] ==0) {

    [temp addObject:[NSNumber numberWithInteger:questionnumber]];
    return r;

}
if ([temp count] >= 1 ){

    if (![temp containsObject:[NSNumber numberWithInteger:questionnumber]]) {

        return r;

    }
    else{

        int next=[self generater];
        return next;
    }
}
return r;
}  

For next Question,

-(void)askQuestion{

[self generater];

questionnumber = r;

NSInteger row = 0;
if(questionnumber == 1)
{
    row = questionnumber - 1;
}
else
{
    row = ((questionnumber - 1) * 11);
}

Can any one suggest me where i am wrong ?

Nick
  • 25,026
  • 7
  • 51
  • 83
iUser
  • 1,075
  • 3
  • 20
  • 49

6 Answers6

4
    arryRandomNumber=[[NSMutableArray alloc]init];
    while (arryRandomNumber.count<12) {
    NSInteger randomNumber=1+arc4random()%12;
    if (![arryRandomNumber containsObject:[NSString stringWithFormat:@"%d",randomNumber]])       {
          [arryRandomNumber addObject:[NSString stringWithFormat:@"%d",randomNumber]];
     }
         continue;
    }
     NSLog(@"%@",arryRandomNumber);
Hemang
  • 1,224
  • 9
  • 15
3

Surely the best approach is to put all the questions (or question numbers) sequentially into an array, and then shuffle them.

See What's the Best Way to Shuffle an NSMutableArray?

Some additional guidance (NB all code written in the browser and not actually compiled and tested; but should still get the gist):

1) Set up an ivar NSMutableArray *randomQuestionNumbers. Initialise the contents of the array with the number of questions, so if you have 5 questions put the numbers 1-5 in. To do it manually it would be:

randomQuestionNumbers = [NSMutableArray arrayWithObjects:[NSNumber numberWithInt:1], [NSNumber numberWithInt:2], [NSNumber numberWithInt:3], [NSNumber numberWithInt:4], [NSNumber numberWithInt:5], nil];

2) After that, you need to shuffle, by using one of the methods given in the link above. That will transform [1,2,3,4,5] -> [4,1,5,3,2] for example 3) So now create your method to get the next question number:

- (NSInteger) nextQuestionNumber {

  NSInteger nextQuestionNumber = -1;
  if ([randomQuestionNumbers count] > 0) {
      nextQuestionNumber = (NSInteger) [randomQuestionNumbers objectAtIndex:0];
      [randomQuestionNumbers removeObjectAtIndex:0];
  }
  return nextQuestionNumber;
}

What's happening here is we retrieve the first int in the array. Then we remove that element from the array. Finally, we return the int, e.g., 4. The next time the method is called, there is one fewer element, and the first element is the int 1, and so on. I'm rather crudely using -1 to show when the array is empty, but to be honest you should be able to ensure you don't call the method more times that necessary.

Community
  • 1
  • 1
andyroberts
  • 3,458
  • 2
  • 37
  • 40
  • Thanks for the link arooaroo. I viewed the link and what i understand is i can exchange object index of array to get random numbers but i don't know how to apply in my case. Can you please give me li'l hint/explanation by some coding that i can use in my case. – iUser May 11 '12 at 09:22
  • Ok, I've added a little more information to steer you in the right direction. – andyroberts May 11 '12 at 10:03
1

Hope this code will help you...

Here limit should be set by your requirement.

Benefit of this code is it won't repeat the same random number which is already been got. (No repetitive numbers)

srand(time(NULL));

int randomIndex = rand() % limit;

[reportIDTextField setText:[NSString stringWithFormat:@"%i", randomIndex]];

Enjoy coding :)

Mrunal
  • 13,982
  • 6
  • 52
  • 96
1

Random number between two numbers method:

- (int)randomFromSmallest: (int) smallest toLargest: (int) largest
{
    int random = smallest + arc4random() % (largest + 1 - smallest);
    return random;
}

And that's how I filling an array of non-repeating numbers:

NSMutableArray *randNrArray = [[NSMutableArray alloc] init];

while ([randNrArray count] < 25)
{
    int rndNumber = [self randomFromSmallest:0 toLargest:25 - 1];

    if (![randNrArray containsObject:[NSNumber numberWithInt:rndNumber]])
    {
        [randNrArray addObject:[NSNumber numberWithInt:rndNumber]];
        //NSLog(@"___ %d ___", rndNumber);
    }
}

I hope this helps.

0

Put all your question numbers in a NSMutableArray (1, 2, 3, 4, 5, 6, 7, 8), then perform a number of permutations on this array.

I use this approach to scramble a numeric keypad:

// Randomize keys: exchange two random keys, ten times.
for (int i = 0; i < 10; i++)
{
    int a = 0, b = 0;
    while (a == b)
    {
        a = arc4random() % 10;
        b = arc4random() % 10;
    }
    UIButton *btnA = (UIButton *)[_scrambledKeypad viewWithTag:a + 10];
    UIButton *btnB = (UIButton *)[_scrambledKeypad viewWithTag:b + 10];
    CGRect frame = btnA.frame;
    btnA.frame = btnB.frame;
    btnB.frame = frame;
}
Cyrille
  • 25,014
  • 12
  • 67
  • 90
0

Maybe : r = [self generater];

questionnumber = r;
Julien
  • 155
  • 4