0

I would like to have an array with 12 numbers -> 0 to 11 the array must be random, and i don't want to have twice the same number

thanx

Ch Hnsn
  • 107
  • 6

3 Answers3

2

Sounds like a shuffling problem.

Just declare an array like follows

NSMutableArray * numbers = [NSMutableArray array];
for (int i = 0; i < 12; i++) {
    [numbers addObject:@i];
}

Then you can shuffle that array using the Fisher-Yates algorithm

for (NSUInteger i = numbers.count - 1; i > 0; --i) {
    NSUInteger n = arc4random_uniform(i+1);
    [numbers exchangeObjectAtIndex:i withObjectAtIndex:n];
}
Gabriele Petronella
  • 106,943
  • 21
  • 217
  • 235
  • I didn’t downvote you, but I think your FY algorithm has something wrong, the way you calculate `n` is a little bit strange, and will probably cause index out of bounds problem. I think you don’t need the final `+ i`. – yonosoytu Jun 23 '13 at 14:00
  • I stand corrected, I changed it with a more reasonable implementation. This is a good comment, as opposed to a useless downvote with no explanation. Thank you – Gabriele Petronella Jun 23 '13 at 14:06
  • And incorrect again (oops): the count down has to be until `i > 0`, since otherwise the final exchange will be between index 0 and index 0, which is not very wise. Then the `arc4random_uniform` has to use `i` as upper limit (inclusive, so `i + 1` in the code). – yonosoytu Jun 23 '13 at 14:09
  • My mistake, I stand corrected again. Thank you for the comments, I don't know when my head is today :) – Gabriele Petronella Jun 23 '13 at 14:13
  • Just follow the link you provided, where it clearly says `j ← random integer with 0 ≤ j ≤ i` (`j` is `n` in your code). If you use the length as upper limit, elements at the start of the array has higher probability of ending at the end. – yonosoytu Jun 23 '13 at 14:15
  • @ChHnsn you're welcome. By the way I've seen your suggested edit. `@i` is modern Objective-C syntax and you should prefer it to the wordy alternative `[NSNumber numberWithInt:i]`. – Gabriele Petronella Jun 25 '13 at 10:13
1

I suggest you create an array and fill it in a loop with the numbers 0 to 11. In a second step, you shuffle that array: What's the Best Way to Shuffle an NSMutableArray?

Community
  • 1
  • 1
PerfectPixel
  • 1,918
  • 1
  • 17
  • 18
-1

You could try something like:

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

    for (int i = 0; i < 12; i++) {

        int randomNumber = min + rand() % (max-min);
        [array addObject:[NSNumber numberWithInt:randomNumber]];  

    }

Not sure if the syntax is correct, im on a Windows machine now

Oleksiy
  • 37,477
  • 22
  • 74
  • 122
  • Well, that will fill and array of 12 elements… all of them being the `NSNumber` 15. Not sure if that’s what the OP was looking for :P – yonosoytu Jun 23 '13 at 13:56
  • What's `min`? What's `max`? And regardless of that, this will produce an array with 12 elements all equals to `15`. Also how does this guarantees uniqueness of the elements? – Gabriele Petronella Jun 23 '13 at 13:56
  • @GabrielePetronella Min and max are whatever you want them to be. It's pretty self-explanatory. Sorry about the typo, I fixed it now – Oleksiy Jun 23 '13 at 13:58
  • @GabrielePetronella Where exactly does he state that he wants every number unique? As far as I know, he just wants a list of random numbers, not necessarily unique. – Oleksiy Jun 23 '13 at 14:00
  • The OP said “i don't want to have twice the same number”. – yonosoytu Jun 23 '13 at 14:02
  • Oh! I must be blind! By the way, what's OP? – Oleksiy Jun 23 '13 at 14:03