# unique random nums - i can't sleep - i've got to know

This is a discussion on unique random nums - i can't sleep - i've got to know within the C++ Programming forums, part of the General Programming Boards category; been working on this simple problem for 3 days. totally frustrated. please, can anybody help me generate unique random nums? ...

1. ## unique random nums - i can't sleep - i've got to know

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;
}

2. I'm not sure what range of numbers you were trying to get, but something like this should work -

Code:
```#include <iostream>
#include <cstdlib>
#include <ctime>

using namespace std;

int main()
{
int randomNums[5] = {-1,-1,-1,-1,-1};

int index = 0;
int temp = 0;

char found = 'n';

int i = 0;
srand(time(NULL) );

while (index < 5)
{
found = 'n';
temp = 1 + rand()%10;

for(i=0;i<index;i++)
{
if (randomNums[i]==temp)
{
found ='y';
break;
}
}

if(found=='n')
randomNums[index++]=temp;
}

for(i=0;i<5;i++)
cout<< randomNums[i]<< endl;

return 0;

}```

3. perfect!!!!!!!
thanks sooooooo much, zen.