1. Welcome to the new MacRumors forums. See our announcement and read our FAQ

object-c 5 different number

Discussion in 'Mac Programming' started by Tehy, Jul 22, 2006.

  1. macrumors regular

    #1
    Hi,

    I got 5 random number:

    srandom(time(NULL));

    luku = random() % 70 + 1;
    luku2 = random() % 70 + 1;
    luku3 = random() % 70 + 1;
    luku4 = random() % 70 + 1;
    luku5 = random() % 70 + 1;

    What would be the best and easiest way to check that all numbers are different numbers?
     
  2. macrumors 6502a

    Vlade

    #2
    Firsts its going to be easier if you make them into an array, then its easy to add more numbers later and makes the code smaller and easier to manage

    int luku[5];
    int i; //loop counter
    srandom(time(NULL));

    for(i=0; i<5; i++) {
    luku = random() % 70 + 1;
    }

    Then to check them add this code

    int j; //Second loop counter
    for(i=0; i<5; i++) {
    for(j=i+1;j<5;j++) {
    if (loku == loku[j]) //Then we have a duplicate
    }
    }
     
  3. macrumors regular

    #3
    Thanks! I will try that :)
     

Share This Page