Skip to content

Commit

Permalink
Add randarg utility source
Browse files Browse the repository at this point in the history
  • Loading branch information
axiom committed Sep 20, 2012
1 parent 30ef6a7 commit 2c3fb59
Showing 1 changed file with 31 additions and 0 deletions.
31 changes: 31 additions & 0 deletions randarg.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#include <stdio.h>
#include <stdlib.h>
#include <sys/time.h>

/*
* Pick one command line argument at random.
*/
int
main(int argc, char **argv)
{
// Make sure there are arguments to choose from.
if (argc == 1) {
fprintf(stderr, "Usage: %s arg1 [arg2 [...]]\n", argv[0]);
return 1;
}

// Get time, including micro seconds.
struct timeval current;
if (gettimeofday(&current, NULL) == -1) {
fprintf(stderr, "Could not get time.\n");
return 1;
}

// Set a random seed.
srand(current.tv_sec + current.tv_usec);
int index = rand() % (argc - 1) + 1;

// Randomize from command line arguments.
fprintf(stdout, "%s", argv[index]);
return 0;
}

0 comments on commit 2c3fb59

Please sign in to comment.