cpp_photon/test/RandGen_test.cpp

78 lines
2.4 KiB
C++
Raw Normal View History

2005-05-15 02:50:07 +00:00
//This file is part of Photon (http://photon.sourceforge.net)
//Copyright (C) 2004-2005 James Turk
//
// Author:
// James Turk (jpt2433@rit.edu)
//
// Version:
2005-08-08 04:55:48 +00:00
// $Id: RandGen_test.cpp,v 1.3 2005/08/08 04:55:48 cozman Exp $
2005-05-15 02:50:07 +00:00
#include <iostream>
#include <iomanip>
#include "photon.hpp"
using namespace photon::util;
using namespace std;
2005-07-19 21:02:04 +00:00
// simple demo of RandGen
2005-08-08 04:55:48 +00:00
int PhotonMain(const photon::StrVec& args)
2005-05-15 02:50:07 +00:00
{
RandGen g1;
2005-07-19 21:02:04 +00:00
RandGen g2(0); // seed randgen 2 and 3 with same number so they are in sync
2005-05-15 02:50:07 +00:00
RandGen g3(0);
2005-07-19 21:02:04 +00:00
const int N=100; // number of iterations
2005-05-15 02:50:07 +00:00
double v[6] = {0};
double s[6] = {0};
double approx[6] = {3.5, 3.5, 3.5, 50, 0, 0.5};
2005-07-19 21:02:04 +00:00
// draw explanation of what each column is
2005-05-15 02:50:07 +00:00
std::cout << "d6 |d6 |d6 | [1,100] | +/- | [0,1) \n";
2005-07-19 21:02:04 +00:00
std::cout << "----------------------------------------------------------\n";
2005-05-15 02:50:07 +00:00
for(int i=0; i < N; ++i)
{
2005-07-19 21:02:04 +00:00
// first 3 columns are 6 sided die
2005-05-15 02:50:07 +00:00
v[0] = g1.genRand(1,6);
v[1] = g2.genRand(1,6);
v[2] = g3.genRand(1,6);
2005-07-19 21:02:04 +00:00
// random scalar between 0 and 100
2005-05-15 02:50:07 +00:00
v[3] = g1.genRand(0.0,100.0);
2005-07-19 21:02:04 +00:00
// random sign (+1 or -1)
2005-05-15 02:50:07 +00:00
v[4] = g1.genRandSign();
2005-07-19 21:02:04 +00:00
// probability [0,1)
2005-05-15 02:50:07 +00:00
v[5] = g1.genRand01();
2005-07-19 21:02:04 +00:00
// do output of all 6 in columns
2005-05-15 02:50:07 +00:00
for(int j=0; j < 6; ++j)
{
std::cout.setf(ios::left);
std::cout << std::setw(9) << v[j] << " ";
2005-07-19 21:02:04 +00:00
s[j] += v[j]; // accumulate sum for each column
2005-05-15 02:50:07 +00:00
}
std::cout << std::endl;
}
2005-07-19 21:02:04 +00:00
// output averages
std::cout << "-average--------------------------------------------------\n";
2005-05-15 02:50:07 +00:00
for(int j=0; j < 6; ++j)
{
std::cout << std::setw(9) << s[j]/N << " ";
}
2005-07-19 21:02:04 +00:00
// output 'expected' averages
std::cout << "\n-expected average---------------------------------------\n";
2005-05-15 02:50:07 +00:00
for(int j=0; j < 6; ++j)
{
std::cout.setf(ios::left);
std::cout << std::setw(9) << approx[j] << " ";
}
std::cout << std::endl;
//Yes I realize what is incorrect about this, "should be near" has no
// meaning since random data can disobey all reason and give you a string
// of 20 billion 6's when rolling a 6-sided die. Most of the time however
// the values will fall within a small deviation from the "expected" and
// they are there for reference when ensuring that the bounds are set
// properly on the generators.
2005-08-08 04:55:48 +00:00
return 0;
2005-05-15 02:50:07 +00:00
}