Thursday, April 12, 2012

How to generate a 3-digit random number in xCode for iPhone

The thing is that I need use a code like this, but it doesn't work with char variable. Can you help me to write the code, or show how we can do it with an array?



Code:



char numeroMaquina[0] = arc4random() % 9;

switch(numeroMaquina[0]) {
case 0:
numeroMaquina[0] = 1;

break;
case 1:
numeroMaquina[0] = 2;

break;
case 2:
numeroMaquina[0] = 3;

break;
case 3:
numeroMaquina[0] = 4;

break;
case 4:
numeroMaquina[0] = 5;

break;
case 5:
numeroMaquina[0] = 6;

break;
case 6:
numeroMaquina[0] = 7;

break;
case 7:
numeroMaquina[0] = 8;

break;
case 8:
numeroMaquina[0] = 9;

break;
}

int numeroMaquina2 = arc4random() % 9;
switch(numeroMaquina2) {
case 0:
numeroMaquina2 = 1;

break;
case 1:
numeroMaquina2 = 2;

break;
case 2:
numeroMaquina2 = 3;

break;
case 3:
numeroMaquina2 = 4;

break;
case 4:
numeroMaquina2 = 5;

break;
case 5:
numeroMaquina2 = 6;

break;
case 6:
numeroMaquina2 = 7;

break;
case 7:
numeroMaquina2 = 8;

break;
case 8:
numeroMaquina2 = 9;

break;
}


int numeroMaquina3 = arc4random() % 9;
switch(numeroMaquina3) {
case 0:
numeroMaquina3 = 1;

break;
case 1:
numeroMaquina3 = 2;

break;
case 2:
numeroMaquina3 = 3;

break;
case 3:
numeroMaquina3 = 4;

break;
case 4:
numeroMaquina3 = 5;

break;
case 5:
numeroMaquina3 = 6;

break;
case 6:
numeroMaquina3 = 7;

break;
case 7:
numeroMaquina3 = 8;

break;
case 8:
numeroMaquina3 = 9;

break;
}




No comments:

Post a Comment