Skip to content

Instantly share code, notes, and snippets.

@justinrixx
Created August 19, 2015 03:12
Show Gist options
  • Save justinrixx/2ee01bd73252e2c384fe to your computer and use it in GitHub Desktop.
Save justinrixx/2ee01bd73252e2c384fe to your computer and use it in GitHub Desktop.
Generate any number of random numbers. Give the desired number to the program as a command line argument.
#include <iostream>
#include <cstdlib>
#include <time.h>
using namespace std;
void usage(const char * name) {
cout << "Usage: " << name << " followed by the number of random digits to display"
<< endl << "e.g. " << name << " 5" << endl;
}
int main(int argc, char ** argv) {
// check the arguments
if (argc != 2) {
usage(argv[0]);
return 1;
}
// get the argument and start the clock
int quantity = atoi(argv[1]);
srand(time(NULL));
// output the random numbers
for (int i = 0; i < quantity; i++) {
cout << (rand() % 100) + 1;
if (i != (quantity - 1)) {
cout << " ";
}
}
cout << endl;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment