/* FILE: randParentString.cpp last change: 1-Jul-2013 author: Romeo Rizzi * This program generates a random string of parenthesis "(" and ")". * Usage syntax: * > randParentString.cpp n seed * where n is the length of the string to be generated. * Usage example: * > randParentString 10 777 */ #include #include #include using namespace std; 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 n = atoi(argv[1]); if(argc > 2) srand( atoi(argv[2]) ); cout << n << endl; for(int i = 1; i <= n; i++) if( RandNumber( 0, 1 ) ) cout << ')'; else cout << '('; cout << endl; return 0; }