[Urgent] Need help, unique random numbers.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ronnchpra
    New Member
    • Dec 2006
    • 4

    [Urgent] Need help, unique random numbers.

    Hello,

    I need help generating 50 unique numbers using rand() BETWEEN 1 and 100 and assign them to a array. I have worked out the number randomization and assignment, but I cant figure out how to generate unique numbers. Can anyone modify this code so that it generates or rather assigns only the unique number to the array:

    #include <cstdlib>
    #include <iostream>
    #include <iomanip>
    #include <time.h>
    #include <cstring>

    using namespace std;

    int main()
    {
    int num,anum[50];
    srand ((int)time(NULL ));

    for (int i=1; i<=50; i++) {
    num = ( 1 + rand() % 100);
    anum[i] = num;
    cout << setw(5) << anum[i];
    if (i % 5 == 0) {
    cout << endl;
    }
    }
    return 0;
    }

    Any help is greatly appreciated.

    thanks
  • Ganon11
    Recognized Expert Specialist
    • Oct 2006
    • 3651

    #2
    Once you have generated the random number, you can have a second for loop (with index j) from 0 to i - 1, checking each previously assigned element of the array. If the element at array[j] equals the element you just assigned (array[i]), then subtract 1 from i (so that the ith element will be randomly assigned again) and break from the second for loop.

    Comment

    • ronnchpra
      New Member
      • Dec 2006
      • 4

      #3
      Originally posted by Ganon11
      Once you have generated the random number, you can have a second for loop (with index j) from 0 to i - 1, checking each previously assigned element of the array. If the element at array[j] equals the element you just assigned (array[i]), then subtract 1 from i (so that the ith element will be randomly assigned again) and break from the second for loop.
      #include <cstdlib>
      #include <iostream>
      #include <iomanip>
      #include <time.h>
      #include <cstring>

      using namespace std;

      int main()
      {
      int num,anum[50];
      srand ((int)time(NULL ));


      for (int i=1; i<=50; i++) {
      num = ( 1 + rand() % 100);
      anum[i] = num;

      for (int j=1; j<=-1; j++) {
      if (anum[j] == anum[i]) {
      i = i-1;
      break;
      }
      }

      cout << setw(5) << anum[i];
      if (i % 5 == 0) {
      cout << endl;
      }
      }
      return 0;
      }

      just tried that man, still no resolve, it still for some reason assigns same numbers to 2 or more array

      Comment

      • ronnchpra
        New Member
        • Dec 2006
        • 4

        #4
        Hmm wait a min, I think I read your response wrong.

        #include <cstdlib>
        #include <iostream>
        #include <iomanip>
        #include <time.h>
        #include <cstring>

        using namespace std;

        int main()
        {
        int num,anum[50];
        srand ((int)time(NULL ));

        for (int i=1; i<=50; i++) {
        num = ( 1 + rand() % 100);
        anum[i] = num;

        for (int j=0; j<=i-1; j++) {
        if (anum[j] == anum[i]) {
        i = i-1;
        break;
        }
        }

        }

        for (int z=1; z<=50; z++) {
        cout << setw(5) << anum[z];
        if (z % 5 == 0) {
        cout << endl;
        }

        }
        return 0;
        }

        WORKED LIKE A CHARM.... thanks a lot man.. thanks a lot

        Comment

        Working...