I'm making a memory game in iOS. There is a 4x4 grid and I want to know the logic of how can I generate random numbers not greater than 4
. These numbers will be assigned to 16
cells of 4x4
grid. But when I apply the functions of random numbers it generate lets say 2
6 times, but in a 4x4 grid I want it to generate one number max 4 times. Here is how I'm doing it:
for (int i=0; i<16; i++) {
int r = arc4random() % 4;
NSLog(@"r = %d at i = %d",r,i);
}
But it generates one number more than 4 times. One more problem I just found that there is a chance that a number never occurs say 0 occurs 4 times, 2 occurs 8 times and 3 occurs 4 times so 1 will be skipped. Kindly address that problem too.