My appologies if this has been answered before but I couldnt find a specific answer/response.

Basically Im wondering how I would assure each random number is one that hasnt been produced already. In the program below Im trying to use the random number produced to act as a specific number of an array.

#include <iostream> 
#include <ctime>
#include <cstdlib>

using namespace std;

int main() 
{ 
int numb[4], n;
int numb2[4];


    srand((unsigned)time(0)); 
  
    int lowest=0, highest=3; 
    int range=(highest-lowest)+1; 

	numb2[0] = 10;
	numb2[1] = 11;
	numb2[2] = 12;
	numb2[3] = 13;
   
		for ( n=0 ; n<4 ; n++ )
		{
                numb[n] = lowest+int(range*rand()/(RAND_MAX + 1.0));
		cout << numb[n] << endl;
		cout << numb2[numb[n]] << endl;
		}
		
	
	
	system("pause");
}

I tried a few if statements and a while loop to assure that if the number drawn is the same as one already drawn then re-do the random number but it wasnt happening.

Any ideas?

do 
{
  numb[2] = lowest+int(range*rand()/(RAND_MAX + 1.0));
}
while ((numb[2] == numb[1]) && (numb[2] == numb[3]) && (numb[2] == numb[0]));

The reason I wish to know how to do this is because for a project I want to randomised the order of how certain strings are shown. I have already used a bubble sort but I was curious for this method.
Safe.

Recommended Answers

All 3 Replies

If you are wanting to contain an array of unique random numbers, then

for each element in the array
    generate a random number
    search the array to see if the number already exists
    if not, then add it to the array
    otherwise, if it was found then go back and generate another number
end of loop

// something like this

const int maxnums= 10;
int nums[maxnums] = {0};
int curnum = 0;
for(int i = 0; i < maxnums; ++i)
{
    bool found = false;
    do {
       int  x = rand();
       for(int j = 0; j < curnum && found == false; ++j)
       {
            if( nums[j] == x)
            {
                found = true;
            }
         }
        if(found == false)
       {
            nums[curnum++] = x;
       }
    } while(found == true);
}
commented: Thank-you +1

Here is another way to do it

#include <iostream>
#include <algorithm>
#include <ctime>
using namespace std;

int main()
{

    const int maxnum = 10;
    int nums[maxnum] = {0};
    srand((unsigned int)time(0));
    int curnum = 0;
    for(int i = 0; i < maxnum; ++i)
    {
        bool found = false;
        do
        {
            int x = rand() % 50;
            int* result = std::find(&nums[0], &nums[maxnum-1], x);
            if( *result < 0)
            {
                nums[curnum++] = x;
                found = true;
            }
        } while( found == false);
   }
    for(int i = 0; i < maxnum; ++i)
        cout << nums[i] << "\n";

}

Thanks kindly Ancient Dragon thats exactly what I wanted!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.