/* FILE: randStrings3.cpp last change: 23-Jan-2013 author: Romeo Rizzi * This program generates 3 random strings. * Usage syntax: * > randStrings3.cpp n1 n2 n3 MIN_CHAR MAX_CHAR seed * where n1, n2 and n3 are the lengths of the 3 strings but when n3 = -1 * then the third string is just the concatenation of the first two. * Usage example: * > randStrings3 10 10 10 A Z 777 */ #include #include #include using namespace std; const int MAX_N1 = 1000, MAX_N2 = MAX_N1; char s1[MAX_N1], s2[MAX_N2]; int RandNumber(int min, int max) { /* returns an integer in [min, max] * see Stroustrup "The c++ Programming Language" 3th edition pg. 685 * for comments on the following manipulation choice. * In particular, considerations on the bad quality of low bits come into account. */ return min + (int) ( (max-min +1) * (double( rand()-0.000000000001 ) / RAND_MAX ) ); } int main(int argc, char** argv) { srand(time(NULL)); int n1, n2, n3 = -1; char MIN_CHAR = 'A', MAX_CHAR = 'B'; n1 = atoi(argv[1]); n2 = atoi(argv[2]); if(argc > 3) n3 = atoi(argv[3]); if(argc > 4) MIN_CHAR = argv[4][0]; if(argc > 5) MAX_CHAR = argv[5][0]; if(argc > 6) srand( atoi(argv[6]) ); cout << n1 << " " << n2 << " " << ( (n3 < 0) ? n1+n2 : n3 ) << endl; for(int i = 1; i <= n1; i++) cout << ( s1[i-1] = (char) RandNumber( (int) MIN_CHAR, (int) MAX_CHAR) ); cout << endl; for(int i = 1; i <= n2; i++) cout << ( s2[i-1] = (char) RandNumber( (int) MIN_CHAR, (int) MAX_CHAR) ); cout << endl; if( n3 >= 0 ) for(int i = 1; i <= n3; i++) cout << (char) RandNumber( (int) MIN_CHAR, (int) MAX_CHAR); else { for(int i = 1; i <= n1; i++) cout << s1[i-1]; for(int i = 1; i <= n2; i++) cout << s2[i-1]; } cout << endl; return 0; }