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
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
Comment