object-c 5 different number

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

  1. macrumors regular

    Joined:
    Jan 15, 2006
    Location:
    Finland
    #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

    Joined:
    Feb 2, 2003
    Location:
    Meadville, PA
    #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. thread starter macrumors regular

    Joined:
    Jan 15, 2006
    Location:
    Finland

Share This Page