Created
June 15, 2015 16:14
-
-
Save devendranaga/ce20a48a3e889dd7d93c to your computer and use it in GitHub Desktop.
random string generator in linux
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdlib.h> | |
#include <string.h> | |
#include <time.h> | |
#include <sys/time.h> | |
char alphabets[] = | |
{'a', 'A', | |
'b', 'B', | |
'c', 'C', | |
'd', 'D', | |
'e', 'E', | |
'f', 'F', | |
'g', 'G', | |
'h', 'H', | |
'i', 'I', | |
'j', 'J', | |
'k', 'K', | |
'l', 'L', | |
'm', 'M', | |
'n', 'N', | |
'o', 'O', | |
'p', 'P', | |
'q', 'Q', | |
'r', 'R', | |
's', 'S', | |
't', 'T', | |
'u', 'U', | |
'v', 'V', | |
'w', 'W', | |
'x', 'X', | |
'y', 'Y', | |
'z', 'Z', | |
}; | |
int main(int argc, char **argv) { | |
struct timeval tv; | |
int seed; | |
int size_alpha = sizeof(alphabets) / sizeof(alphabets[0]); | |
int string_len = atoi(argv[1]); | |
int i; | |
gettimeofday(&tv, 0); | |
char *output = calloc(1, string_len + 1); | |
if (!output) | |
return -1; | |
seed = tv.tv_usec / 1000; | |
srand(seed); | |
for (i = 0; i < string_len; i++) { | |
output[i] = alphabets[rand() % size_alpha]; | |
} | |
output[i] = '\0'; | |
printf("%s\n", output); | |
free(output); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment