/* * generate a random readable string * */ #include #include #include struct _charweight { char Ch; int Type; int Weight; }; #define T_VOWEL 1 #define T_CONS 2 struct _charweight charweights [] = { {'a', T_VOWEL, -5}, {'b', T_CONS, 5}, {'c', T_CONS, 5}, {'d', T_CONS, 5}, {'e', T_VOWEL, -4}, {'f', T_CONS, 5}, {'g', T_CONS, 4}, {'h', T_CONS, 3}, {'i', T_VOWEL, -4}, {'j', T_CONS, 5}, {'k', T_CONS, 5}, {'l', T_CONS, 3}, {'m', T_CONS, 3}, {'n', T_CONS, 3}, {'o', T_VOWEL, -4}, {'p', T_CONS, 5}, {'q', T_CONS, 5}, {'r', T_CONS, 4}, {'s', T_CONS, 3}, {'t', T_CONS, 4}, {'u', T_VOWEL, -4}, {'v', T_CONS, 4}, {'w', T_CONS, 4}, {'x', T_CONS, 6}, {'y', T_CONS, 5}, {'z', T_CONS, 5} }; int randomchar (void) { unsigned long T; /* yet another stupid random generator - YASGR ! */ T = random () - ((random () & 0xFFFFL) << 8) + (time(0) & 0x3FF); T = (T & 0x00FFL) + ((T & 0xFF00L) >> 8); return (T % 26); } char * random_readable_string (int targetweight) { int Cnt = 16; int BufCnt = 0; int Container; int weight = 0; int oldweight; int conscnt = 0; int vowelcnt = 0; static char Buf[128]; while (Cnt) { Container = randomchar (); oldweight = weight; weight += charweights[Container].Weight; /* Check if the coin would add an acceptable weight if not keep looking */ if ((weight > targetweight) || (weight <= 0)) { weight = oldweight; continue; } /* check if it we already have three consonants or vowels */ if ((charweights[Container].Type) == T_VOWEL) { conscnt = 0; if (vowelcnt == 2) { /* we don't allow more than 2 vowels */ continue; } else { vowelcnt += 1; } } else { vowelcnt = 0; if (conscnt == 2) { /* already enough consonants -- keep looking */ continue; } else { conscnt += 1; } } Buf[BufCnt] = charweights[Container].Ch; Buf[BufCnt+1] = 0; BufCnt += 1; Cnt -= 1; } return Buf; } int main (int argc, char **argv) { int targetweight = 0; int cnt; srandom ((time(0) & 0x3FFFF)+1); if (argc > 1) { targetweight = atoi (argv[1]); #ifdef DEBUG printf ("argc = %d\n", argc); printf ("Initial TargetWeight is %d\n", targetweight); #endif } if (targetweight == 0) { #ifdef TARGET_WEIGHT targetweight = TARGET_WEIGHT; #else targetweight = 9; #endif } if (argc > 2) { cnt = atoi (argv[2]); if (cnt == 0) { cnt = 10; } while (cnt) { printf ("%s\n", random_readable_string (targetweight)); cnt -= 1; } } else { printf ("%s\n", random_readable_string (targetweight)); } return 0; }