Skip to content

Instantly share code, notes, and snippets.

@kkrolikowski
Created March 3, 2015 20:58
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kkrolikowski/46ff7c449ac54fff3b9a to your computer and use it in GitHub Desktop.
Save kkrolikowski/46ff7c449ac54fff3b9a to your computer and use it in GitHub Desktop.
random filename
/*
* randomize.c -- generate random file name
* Author: Krzysztof Krolikowski <kkrolikowski@gamil.com>
* compilation:
* gcc -o randomize randomize.c
* running:
* ~$ ./randomize
* dyndns_nBXGV0cW
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>
int main(void) {
char * entropy = "1qaz2wsx3edc4rfv5tgb6yhn7ujm8i0poklaZAQ1XSW2CDE3VFR4BGT5NHY6MJU6MJU7";
int n, i;
const int len = 8; // random part lenght
struct timeval tv;
char tmp[15] = "dyndns_"; // prefix of temp file
char * ptmp; // pointer to the begining of temp filename
ptmp = tmp + strlen(tmp); // moving to the end of a prefix
for(i=0; i<len; i++, ptmp++) { // while counter is smaller then lenght of random part
gettimeofday(&tv, NULL); // get current time
srand(tv.tv_usec); // generate new seed with time in micro seconds
n = rand() % strlen(entropy); // generate new random character
*ptmp = entropy[n]; // and put it in the next field of an array.
}
*ptmp = '\0'; // end of string containing random filename
puts(tmp);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment