Unique Random Numbers Array

So I've been struggling with a writing a program for homework for a week and a half that requires an RNG to create six random numbers for an array with the maximum number selectable as chosen by the user.

That part was easy enough and I finished that in about 20 minutes, but the final part of the program has given me grief for a week and a half: I must find a way to ensure that all of these randomly generated numbers are all unique. Since there's only six numbers, I'm sure a nested for loop would work (Probably pretty bad to use for large arrays, but should be fine since it's just 6 elements), but I'm just not sure how I'd ask the loops to roll through each element of an array and check that each one is unique and reselect another number if it's not. I'd really like a bit of help if I could get it on what I could do.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#include <cstdlib>
#include <iostream>
#include <ctime>
#include <iomanip>

using std::cout;
using std::cin;
using std::endl;
using std::setw;

int getRandom ( int Min, int Max );

char ch;

int userMax = 0;

int main()
{
    srand( time ( NULL ) );

    int lotteryArray[ 6 ];

    cout << "Greetings user! What is the maximum possible number you would like for your lottery range? ";
    cin >> userMax;

    do
    {
        for ( int i = 0; i < 6; i++ )
        {
            lotteryArray[ i ] = getRandom( 1, 6 );
        }
        cout << "Here are your lottery numbers from 1 to " << userMax << "." << endl << endl;
        for ( int index = 0; index < 6; index++ )
        {
            cout << lotteryArray[ index ] << setw( 5 );
        }
        cout << endl << "Do you want to play again? Press 'Y' for Yes or any other letter to exit. ";
        cin >>  ch;
    }
    while( ch == 'Y' || ch == 'y' );

    return EXIT_SUCCESS;
}

int getRandom ( int Min, int Max )
{
    int number = rand() % userMax + 1;
    return number;
}


Thanks in advance.
closed account (Dy7SLyTq)
std::vector
If you have an array that you need to fill with unique numbers, and you have as many elements as unique numbers, you want to assign each element a number (element 0 is 1, element 1 is 2, element 2 is 3, etc) then shuffle those elements so that their place in the array is random.

Searching on "random shuffle" may prove informative.
Unfortunately not cire. Sorry for the late response. As the code above says, it's up to the user to specify what the maximum possible unique number can be, so it could be six, or it could be fifty thousand. There's only 6 printouts. I'm trying to stay away from vectors, classes and advanced structures since this is a simple 'proof of concept' idea.

Could you or anyone else suggest something such as a way of using nested loops or something fairly simplistic (regardless of efficiency)?
Well, you already have it so that the array fills each spot with a random number. You just need another for loop that checks that random number against all other numbers already in the array- you could actually use a separate proc here that has the current value of i, the random number, and the current numbers in the array (just the entire array) passed to it. It would then do a check for each value of i incremented down each time until it hits 0. If the random number never equals any value from the array, then it should assign that value to whatever array spot it should go to (could make the proc return a boolean true or false, and have it immediately break out of the the loop the moment that the random value equals one from the array to make it more efficient).
2 ways.
1. Shuffle array of sequential values.
2. every time check if value exist in array.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
for(int i=0;i<size;i++)
{
    bool unique;
    do
    {
      unique=true;
      newitem=rand()%100;
      for(int i1=0;i1<i;i1++)
      {
         if(array[i1]==newitem)
         {
           unique=false;     
            break
         }
      }
    }while(!unique);
    array[i]=newitem;

}
Fantastic, thank you both. With that I've managed to crack it and get it functioning. Much appreciated! :)
Topic archived. No new replies allowed.