Wednesday, August 3, 2011

Objective-C: Array creating with no repeated numbers in

Dudes & Ladies,


Here's a piece of code that just came at me for a game we're bringing up.
What does it do? It creates a array with any number of possible positions and stocks aleatory non-repeated numbers in each index.
Cool, huh?


CODE:

-(void)createArrayDude{

    int AN=1, indx = 36, array[indx], arcvalue; 
    
    for (int k=0; k<=(indx-1); k++){
        jumpLabel:
        arcvalue = arc4random()%(indx+1);
        if (arcvalue == 0){
            goto jumpLabel;
        }
        for (int m=0; m<=k; m++){
            if (arcvalue == array[m]){
                goto jumpLabel;
            }
        }
        array[k] = arcvalue;
       //CMT01
       [self saveArrayNumber:AN stocking:array[k] atIndex:k];
        NSLog(@"ARRAY[%d]=%d", k, array[k]);
    }
}


CMT01: The line under saves the array right in the memory(kind of visible because of the name, eh?)

In the indx you can use any number.
Just be happy.

No comments:

Post a Comment

Tweet