#include #include #include // ランダムな文字列を生成する関数 char* generateRandomString(int length) { const char charset[] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!@#$%^&*()_-+=<>?"; char* randomString = (char*)malloc((length + 1) * sizeof(char)); if (randomString == NULL) { fprintf(stderr, "メモリの割り当てエラー\n"); exit(EXIT_FAILURE); } for (int i = 0; i < length; i++) { // sizeof(charset) は文字セットのサイズに1を追加してしまうため 1 引く int index = rand() % (sizeof(charset) - 1); randomString[i] = charset[index]; } randomString[length] = '\0'; // 文字列の終端を設定 return randomString; } int main() { // 協定世界時 (UTC) での1970年1月1日午前0時からの経過秒数を返す srand((unsigned int)time(NULL)); // 乱数の初期化(srand 関数は、そのシードとして符号なし整数を受け取る) int passwordLength = 12; // パスワードの長さを設定 char* password = generateRandomString(passwordLength); printf("生成されたパスワード: %s\n", password); free(password); // メモリの解放 return 0; }