loop randomly

Is it possible to loop randomly
for example
for ( int i = 0; i<= 6 ; i++ )
I don't want i to acsend from 0 to 6
but i want it to get all numbers randomly
for example
first time r = 5 second time r = 2 and so on
until it gets all the seven numbers
sry for my terrible english :D
Create temporary array and fill it with numbers from 0 to 6, then shuffle it any way you like and then do something like:
1
2
3
4
int t[] = // shuffled array
for (int j = 0; j <= 6; j++) {
    int i = t[j];
}


You can built-in shuffling if you like, for example with this simple algorithm:
http://codeabbey.com/index/task_view/cards-shuffling

More interesting approach is to create random generator for index. You can read the simple theory of LGC here:
http://codeabbey.com/index/wiki/random-numbers
You need to think thoroughly of constraints - for example for your case M would be 7 and A could be K*7 + 1, while C could be any number not divisible by 7, so:
1
2
3
M = 7;
A = rand() % 100000 * 7 + 1;
C = rand() % 100000 * 7 + rand() % 6 + 1;

would give you lots of variants.
This approach allows you to avoid using temporary array.
Wow ! ... Thx radiongorky ... Shuffling an array is exactly what I wanted
and thx ats15 too :)
@lordahmed then you should have asked that directly....
Topic archived. No new replies allowed.