RandGen test reviewed
This commit is contained in:
parent
4de39b5a2f
commit
f0aeb3522f
@ -5,7 +5,7 @@
|
|||||||
// James Turk (jpt2433@rit.edu)
|
// James Turk (jpt2433@rit.edu)
|
||||||
//
|
//
|
||||||
// Version:
|
// Version:
|
||||||
// $Id: RandGen_test.cpp,v 1.1 2005/05/15 02:50:07 cozman Exp $
|
// $Id: RandGen_test.cpp,v 1.2 2005/07/19 21:02:04 cozman Exp $
|
||||||
|
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <iomanip>
|
#include <iomanip>
|
||||||
@ -13,40 +13,52 @@
|
|||||||
using namespace photon::util;
|
using namespace photon::util;
|
||||||
using namespace std;
|
using namespace std;
|
||||||
|
|
||||||
|
// simple demo of RandGen
|
||||||
int main()
|
int main()
|
||||||
{
|
{
|
||||||
RandGen g1;
|
RandGen g1;
|
||||||
RandGen g2(0);
|
RandGen g2(0); // seed randgen 2 and 3 with same number so they are in sync
|
||||||
RandGen g3(0);
|
RandGen g3(0);
|
||||||
const int N=100;
|
const int N=100; // number of iterations
|
||||||
double v[6] = {0};
|
double v[6] = {0};
|
||||||
double s[6] = {0};
|
double s[6] = {0};
|
||||||
double approx[6] = {3.5, 3.5, 3.5, 50, 0, 0.5};
|
double approx[6] = {3.5, 3.5, 3.5, 50, 0, 0.5};
|
||||||
|
|
||||||
|
// draw explanation of what each column is
|
||||||
std::cout << "d6 |d6 |d6 | [1,100] | +/- | [0,1) \n";
|
std::cout << "d6 |d6 |d6 | [1,100] | +/- | [0,1) \n";
|
||||||
std::cout << "---------------------------------------------------------------\n";
|
std::cout << "----------------------------------------------------------\n";
|
||||||
for(int i=0; i < N; ++i)
|
for(int i=0; i < N; ++i)
|
||||||
{
|
{
|
||||||
|
// first 3 columns are 6 sided die
|
||||||
v[0] = g1.genRand(1,6);
|
v[0] = g1.genRand(1,6);
|
||||||
v[1] = g2.genRand(1,6);
|
v[1] = g2.genRand(1,6);
|
||||||
v[2] = g3.genRand(1,6);
|
v[2] = g3.genRand(1,6);
|
||||||
|
// random scalar between 0 and 100
|
||||||
v[3] = g1.genRand(0.0,100.0);
|
v[3] = g1.genRand(0.0,100.0);
|
||||||
|
// random sign (+1 or -1)
|
||||||
v[4] = g1.genRandSign();
|
v[4] = g1.genRandSign();
|
||||||
|
// probability [0,1)
|
||||||
v[5] = g1.genRand01();
|
v[5] = g1.genRand01();
|
||||||
|
|
||||||
|
// do output of all 6 in columns
|
||||||
for(int j=0; j < 6; ++j)
|
for(int j=0; j < 6; ++j)
|
||||||
{
|
{
|
||||||
std::cout.setf(ios::left);
|
std::cout.setf(ios::left);
|
||||||
std::cout << std::setw(9) << v[j] << " ";
|
std::cout << std::setw(9) << v[j] << " ";
|
||||||
s[j] += v[j];
|
s[j] += v[j]; // accumulate sum for each column
|
||||||
}
|
}
|
||||||
std::cout << std::endl;
|
std::cout << std::endl;
|
||||||
}
|
}
|
||||||
std::cout << "-average-------------------------------------------------------\n";
|
|
||||||
|
// output averages
|
||||||
|
std::cout << "-average--------------------------------------------------\n";
|
||||||
for(int j=0; j < 6; ++j)
|
for(int j=0; j < 6; ++j)
|
||||||
{
|
{
|
||||||
std::cout << std::setw(9) << s[j]/N << " ";
|
std::cout << std::setw(9) << s[j]/N << " ";
|
||||||
}
|
}
|
||||||
std::cout << "\n-should be near------------------------------------------------\n";
|
|
||||||
|
// output 'expected' averages
|
||||||
|
std::cout << "\n-expected average---------------------------------------\n";
|
||||||
for(int j=0; j < 6; ++j)
|
for(int j=0; j < 6; ++j)
|
||||||
{
|
{
|
||||||
std::cout.setf(ios::left);
|
std::cout.setf(ios::left);
|
||||||
|
Loading…
Reference in New Issue
Block a user