/*
Science and technology promotion license applied.
(c) Zhikai Wang/ www.heteroclinic.net 2013
*/
#include <iostream>
#include <cstdlib>
#include <climits>
#include <string.h>
// ULLONG_MAX should be 2^64-1 = 1.8446744073709551615e+19
// 20! is 2.43290200817664e+18
// 21! is 5.109094217170944e+19 //overflow
// So it is not possible to use index permutation, we still need shuffling.
// c is the length
void shuffleAnArray (int c, int * myarray) {
	int array_i=0; 
	for (array_i=0; array_i < c-1 ; array_i++) {
		int v = rand() % (c -array_i-1);
		int cursor = v+array_i+1;
		if (cursor >= c)
			continue;
		int tmp = myarray[cursor ];
		myarray[cursor ] = myarray[array_i];
		myarray[array_i] = tmp;
			// int j = 0;
			// for ( ; j <c ; j++) {
			// std::cout<<myarray[j]<<"\t";
			// }
			// std::cout<<std::endl;
			
			//myarray[array_i] = array_i + 1;
	}
}

int main () {
	srand(time(NULL));
	int * array = new int[40];
	int i = 0; 
	for (i = 0; i< 40; i++ ) {
		array[i] = i+1;
	}
	shuffleAnArray (40, array);
	for (i = 0; i< 40; i++ ) {
		std::cout<<array[i]<<std::endl;
	}

	return 0;
}