1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
|
#include <boost/random/uniform_int.hpp>
#include <boost/random/mersenne_twister.hpp>
#include <boost/random/variate_generator.hpp>
#include <ctime>
#include <limits>
#include <string>
#include <iomanip>
#include <cstdlib>
#include <iostream>
// Global Gen Var
boost::mt19937 gen;
// Prototypes
int rand(int, int);
void random_statistics(std::string, unsigned long long, std::string, int, int, bool);
// BEGIN
int main(int argc, char * argv[])
{
srand ((unsigned)std::time(0)); // Seed
gen.seed((unsigned)std::time(0)); // Seed
const int mod = 3;
const int top = 1;
// CSTDLIB
random_statistics("std::rand()", RAND_MAX, "Using % Opperator", top, mod, false);
// BOOST LIB 1
random_statistics("boost::rand():", RAND_MAX, "Limiting to CSTDLIB MAX, USING % METHOD", top, mod, true);
// BOOST LIB 2
random_statistics("boost::rand():", std::numeric_limits<int>::max(), "Limiting to MAX INT, USING % METHOD", top, mod, true);
// BOOST LIB 3
random_statistics("boost::rand():", mod-1, "LIMITING TO 3, Directly randomizing [0, 3)", top, mod, true);
return 0;
}
int rand(int min, int max)
{
boost::uniform_int<> dist(min, max);
boost::variate_generator< boost::mt19937&, boost::uniform_int<> > rnd(gen, dist);
return rnd();
}
void random_statistics(std::string mtd, unsigned long long max, std::string useage, int top, int mod, bool ver)
{
std::cout << "// ===================================================== //" << std::endl;
std::cout << "MAXIMUM RANDOM NUMBER VIA " << mtd << " " << max << std::endl;
std::cout << useage << std::endl << std::endl;
int var, count = 0;
double chance;
const int runs = 100000;
for (int i = 0; i < runs; ++i)
{
(ver) ? ( var = rand(0, max) ) : ( var = std::rand() );
if ( (var % mod) < top )
++count;
}
chance = double(count) / double(runs);
std::cout << "Percent chance of getting < " << top << ": " << std::fixed << std::setprecision(3)
<< chance << "%" << std::endl;
std::cout << "This should be close to a " << top << "/" << mod << " chance!" << std::endl << std::endl;
}
|
// ===================================================== //
MAXIMUM RANDOM NUMBER VIA std::rand() 32767
Using % Opperator
Percent chance of getting < 1: 0.332%
This should be close to a 1/3 chance!
// ===================================================== //
MAXIMUM RANDOM NUMBER VIA boost::rand(): 32767
Limiting to CSTDLIB MAX, USING % METHOD
Percent chance of getting < 1: 0.333%
This should be close to a 1/3 chance!
// ===================================================== //
MAXIMUM RANDOM NUMBER VIA boost::rand(): 2147483647
Limiting to MAX INT, USING % METHOD
Percent chance of getting < 1: 0.333%
This should be close to a 1/3 chance!
// ===================================================== //
MAXIMUM RANDOM NUMBER VIA boost::rand(): 2
LIMITING TO 3, Directly randomizing [0, 3)
Percent chance of getting < 1: 0.333%
This should be close to a 1/3 chance!
Process returned 0 (0x0) execution time : 0.156 s
Press any key to continue.
// RUN 2
// ===================================================== //
MAXIMUM RANDOM NUMBER VIA std::rand() 32767
Using % Opperator
Percent chance of getting < 1: 0.335%
This should be close to a 1/3 chance!
// ===================================================== //
MAXIMUM RANDOM NUMBER VIA boost::rand(): 32767
Limiting to CSTDLIB MAX, USING % METHOD
Percent chance of getting < 1: 0.333%
This should be close to a 1/3 chance!
// ===================================================== //
MAXIMUM RANDOM NUMBER VIA boost::rand(): 2147483647
Limiting to MAX INT, USING % METHOD
Percent chance of getting < 1: 0.334%
This should be close to a 1/3 chance!
// ===================================================== //
MAXIMUM RANDOM NUMBER VIA boost::rand(): 2
LIMITING TO 3, Directly randomizing [0, 3)
Percent chance of getting < 1: 0.335%
This should be close to a 1/3 chance!
Process returned 0 (0x0) execution time : 0.161 s
Press any key to continue. |