mercredi 15 mars 2017

Get sequence of random numbers' pairs (Objective-c)

Good morning, i'm trying to generate a sequence of N pairs of numbers, for example 1-0, 2-4, 4-3. These numbers must range between 0 and 8 and the pair must be different for all the numbers.

I don't want that: 1-3 1-3

I found that if a and b are the numbers, (a+b)+(a-b) has to be different for all couples of numbers.

So I manage to do that, but the loop never ends. Would you please correct my code or write me another one? I need it as soon as possible.

           NSNumber*number1;

int risultato;
int riga;
int colonna;
NSMutableArray*array=[NSMutableArray array];
NSMutableArray*righe=[NSMutableArray array];
NSMutableArray*colonne=[NSMutableArray array];

for(int i=0; i<27; i++)
{
    riga=arc4random()%9;
    colonna=arc4random()%9;
    risultato=(riga+colonna)+(riga-colonna);
    number1=[NSNumber numberWithInt:risultato];
    while([array containsObject:number1])
    {
        riga=arc4random()%9;
        colonna=arc4random()%9;
        risultato=(riga+colonna)+(riga-colonna);
        number1=[NSNumber numberWithInt:risultato];
    }

    NSNumber*row=[NSNumber numberWithBool:riga];
    NSNumber*column=[NSNumber numberWithInt:colonna];
    [righe addObject:row];
    [colonne addObject:column];
    [array addObject:number1];

}

for(int i=0; i<27; i++)
{
    NSNumber*one=[righe objectAtIndex:i];
    NSNumber*two=[colonne objectAtIndex:i];

    NSLog(@"VALUE1 %ld VALUE2 %ld", [one integerValue], (long)[two integerValue]);
}

Thanks!




Aucun commentaire:

Enregistrer un commentaire