Skip to content

Instantly share code, notes, and snippets.

@perillamint
Created April 10, 2013 13:37
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 perillamint/5354695 to your computer and use it in GitHub Desktop.
Save perillamint/5354695 to your computer and use it in GitHub Desktop.
Random string generator.
//Released under GNU GPL v3.
#include <stdio.h>
static const char alphanum[] =
"0123456789"
"!@#$%^&*"
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
"abcdefghijklmnopqrstuvwxyz";
int stringLength = sizeof(alphanum) - 1;
FILE *fd_rand;
unsigned int hwrandom()
{
int data;
char *d = (char*)&data;
int i;
for(i=0; i<4; i++)
{
*d = fgetc(fd_rand);
d++;
}
return data;
}
char genRandom()
{
return alphanum[hwrandom() % stringLength];
}
int main()
{
srand(time(0));
fd_rand = fopen("/dev/urandom", "r");
int i;
int loop = hwrandom()%15+1;
for(i=0; i<loop; i++)
printf("%c", genRandom());
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment