前にも Ruby や Python でやったネタだけども。
#include #include #include #include int main(int argc, char *argv[]) { int i; char strpool[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"; int lenpool = strlen(strpool); int l, r; l = atoi(argv[1]); char strrand[l + 1]; srand((unsigned) time(NULL)); for (i = 0; i < l; i++) { r = rand() % lenpool; strrand[i] = strpool[r]; } strrand[l] = '\0'; printf("%s\n", strrand); return 0; }
takatoh@nightschool $ gcc -Wall -o strrand strrand.c takatoh@nightschool $ ./strrand 20 hiVKZhO2rwtlDPUJmdo7