Skip to content

Instantly share code, notes, and snippets.

@devendranaga
Created June 15, 2015 16:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save devendranaga/ce20a48a3e889dd7d93c to your computer and use it in GitHub Desktop.
Save devendranaga/ce20a48a3e889dd7d93c to your computer and use it in GitHub Desktop.
random string generator in linux
#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