Random numbers without repetition

I am creating a Quiz application in which I want to generate random numbers without repeating.
I searched a lot of things and got an idea, but I think that I am doing something wrong, why not get the right result.

Here is the code 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;
}  

In the next question

-(void)askQuestion{

[self generater];

questionnumber = r;

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

Can someone suggest me where I'm wrong?

+3
source share
6 answers
    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);
+4
source

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

See What is the best way to swap in NSMutableArray?

(NB , , , ):

1) ivar NSMutableArray *randomQuestionNumbers. , , 5 , 1-5 . :

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

2) , , . [1,2,3,4,5] → [4,1,5,3,2], 3) , :

- (NSInteger) nextQuestionNumber {

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

, int . . , int, , 4. , , , - int 1 .. -1, , , , , , , .

+2

, ...

.

, , . ( )

srand(time(NULL));

int randomIndex = rand() % limit;

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

:)

+1

:

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

:

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);
    }
}

, .

+1

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

I use this approach to scramble the 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;
}
0
source

Perhaps: r = [self-generator];

questionnumber = r;
0
source

All Articles