Im trying to produce an array with 4 random generated [srand seeded rand()] without any repeats. Im using a for loop to:
Select a position in the array, Generate a number, Assign the number to the currently highlighted position Check that the assigned number is not equal to a previous entry
if no -
Then select the next position in the array and generate a new number
if yes -
Do not move to the next array position and generate a new number again.
repeat until array position 3
This was my attempt:
int operator_selection;
int operator_index[3];
int random_value;
for (operator_selection = 0; operator_selection < 4; operator_selection++)
{
random_value = rand() %4 + 1;
if (random_value = operator_index[0] || operator_index[1] || operator_index[2])
{
(operator_selection - 1);
}
operator_index[operator_selection] = random_value;
cout<<operator_index[operator_selection]<<" ";
if (operator_selection == 3)
{
cout<<endl;
}
}
However when I run the executable I always end up with repeats, so Im pretty sure the logic behind my first 'if statement' is flawed.
Im a c++ begineer and this is my 3rd attempt at writing a source file from scratch, so appologies if I've made a silly mistake.
Aucun commentaire:
Enregistrer un commentaire