Skip to content

Instantly share code, notes, and snippets.

@o11c
Created May 29, 2013 03:59
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 o11c/5667876 to your computer and use it in GitHub Desktop.
Save o11c/5667876 to your computer and use it in GitHub Desktop.
A really WTF way of generating a random entry out of a list of numbers.
int main(int argc, char **argv)
{
int fd = open("/dev/urandom", 0);
int ai = 0;
int idx;
for (;;)
{
char c;
read(fd, &c, 1);
if (ai && argv[ai][idx + 1] == c)
{
printf("++ %d %d\n", ai, idx);
idx++;
if (idx == strlen(argv[ai]))
break;
}
for (int i = 1; i < argc; ++i)
if (argv[i][0] == c)
{
printf("-> %d %d\n", ai, idx);
ai = i;
idx = 0;
}
}
puts(argv[ai]);
return ai;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment