Skip to content

Instantly share code, notes, and snippets.

@LightningStalker
Created November 21, 2024 10:48
Show Gist options
  • Select an option

  • Save LightningStalker/24df9fa9f996fa3c164c7d6970a5fc9e to your computer and use it in GitHub Desktop.

Select an option

Save LightningStalker/24df9fa9f996fa3c164c7d6970a5fc9e to your computer and use it in GitHub Desktop.
Generates a random number between 1 and argv[1]
/* rngesus Generates random numbers >= 1 <= argv[1]
*
* Project Crew 11/21/2024
*/
#include <iostream>
#include <exception>
#include <random>
using namespace std;
using u32 = uint_least32_t;
using engine = mt19937;
int
main(int argc, char * argv[])
{
int lim;
if (argc == 2)
{
random_device os_seed;
const u32 seed = os_seed();
engine generator(seed);
try
{
lim = stoi(argv[1]);
} catch (invalid_argument& e)
{
cout << "error: I seeketh thine natural number." << endl;
return EXIT_FAILURE;
}
if (lim < 2)
{
cout << "error: GOD shall forgiveth thee for coveting such small numbers." << endl;
return EXIT_FAILURE;
}
uniform_int_distribution < u32 > distribute(1, lim);
// for (int repetition = 0; repetition < 10; ++repetition)
{
cout << distribute(generator) << endl;
}
return EXIT_SUCCESS;
}
cout << endl
<< " I cannot bring forth a random number because:" << endl
<< endl
<< " Wrong number of arguments: $ " << argv[0] << " [ upper limit ]" << endl
<< endl;
return EXIT_FAILURE;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment