How can I make a random array?
Just for example:
I have this:
a, b , c , d , e ,f
I want the computer to make any pattern using these. It should give me 4 patterns.
Well what I am trying to do is get set of 4 different colours from the six different colors I have.
There is a good algorithm for this in the STL. This is the code for what you want, it might look a bit daunting, but it's not so difficult. I have put comments to explain what is happening. Ask about anything you do not understand.
//These are the header files we need.
#include <iostream>
#include <algorithm> //<-- I suggest reading up a bit about this. It has some very useful stuff in!
#include <vector> //<-- This gives you a variable size array. Very useful and worth reading about too.
using std::endl; //To save some typing later.
using std::cout;
using std::vector;
int main()
{
shortint x = 0, y = 0; //Our variables to iterate around loops.
vector<int> v; //This is an integer array that can be of variable size.
while(x < 6) //This loop puts into the array 1,2,3,4,5,6.
{
v.push_back(x + 1); //Put (x + 1) into the array at the end.
++x;
}
while(y < 4) //Because you want 4 sets of numbers.
{
random_shuffle (v.begin(), v.end()); //Shuffle our numbers. This is from the library <algorithm>
x = 0; //Just a loop to print out the newly shuffled elements.
while(x < 6)
{
cout << v[x];
++x;
}
cout << endl;
++y;
}
return 0; //Done
}