Creating a Random array

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.

Any ideas?
First do http://en.wikipedia.org/wiki/Fisher–Yates_shuffle , then take first 4 elements.
You phrased your question twice in two different ways having different meanings...

It should give me 4 patterns.
This would mean, you want 4 sets of shuffled elements from 6 elements
Eg:
a, c, b, e, d, f
b, a, e, d, c, f
f, e, b, a, d, c
d, b, a, e, c, f

get set of 4 different colours from the six different colors I have.
This would mean you want one set of 4 random elements from an array of 6 elements..
Eg:
b, d, a, e


So what exactly do you want?
I want
a,b,c,d
c,d,e,f
d,e,f,c
c,d,e,f
however, the elements can only be from a b c d e and f.



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.
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
//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()
{
    short int 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
}
Last edited on
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
struct colours
{                        
       int red;
       int green;
       int blue;
           
}sblue, sgreen, slblue, syellow, sred, swhite;
//--------------------------------------------------------------------------------------------------------------------
int createrandomcolors()
{
    short y = 0;
    sblue.red = 0; sblue.green = 0; sblue.blue = 255;
    sgreen.red = 0; sgreen.green = 100; sgreen.blue = 0;
    slblue.red= 0; slblue.green = 255; slblue.blue =255;
    syellow.red =265; syellow.green = 140; syellow.blue = 0;
    sred.red =1; sred.green = 0; sred.blue = 0;
    swhite.red=1; swhite.green = 1; swhite.blue =1;
    colours array[6] = {sblue, sgreen, slblue, syellow, sred, swhite};
    while(y < 4)
    {
            random_shuffle (sblue, sgreen, slblue, syellow, sred, swhite);
            y++;           
    }
       
}

could you please tell why this does not work.
Last edited on
will this work?

colours sixcolours[6] = {sblue, sgreen, slblue, syellow, sred, swhite};
coulours fourcolours[4];
colours selected[4];
int complete=0;
int random;
int chosen=0;
int selectedcounter = 0;

do{ random = rand()%6;

for (int i = 0; i < 4; i++)
{
if (selected[i] != random)
selectedcounter++;
if (selectedcounter == 3)
{
selected[chosen] = sixcoulours[random];
fourcolours[chosen]= sixcoulours[random];
chosen++;
}
}
if (chosen == 3)
complete = 1
}while (complete!=1);
thanks from all this I have got the conclusion :D
Thank you very much people.
Topic archived. No new replies allowed.