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.