been working on this simple problem for 3 days. totally frustrated.
please, can anybody help me generate unique random nums?
only been programming w/ c++ 4-1/2 months. have read the tuts, coder, but don't understand. anyway, here's my code, and it won't generate unique nums.
//UNIQUE
#include <iostream.h>
#include <stdlib.h>
#include <time.h>
void main()
{
int randomNums[5] = {-1};
int index = 0;
int temp = 0;
char found = 'n';
int i = 0;
srand(time(NULL) );
while (index < 5)
{
found = 'n';
for (i; i < 5 ; i = i + 1)
{
temp = 1 + rand() % (10 - 1 + 1);
if (randomNums[index] == temp)
{
found ='y';
}
else
if(found = 'n')
{
randomNums[index] = temp;
index = index + 1;
}
}
}
cout<< randomNums[0]<< endl;
cout<< randomNums[1]<< endl;
cout<< randomNums[2]<< endl;
cout<< randomNums[3]<< endl;
cout<< randomNums[4]<< endl;
}



LinkBack URL
About LinkBacks


