Basic Image AlgorithmS Library  2.8.0
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
ExampleRandom.cpp

Example for random number generator

Author
MIP
/*
This file is part of the BIAS library (Basic ImageAlgorithmS).
Copyright (C) 2003-2009 (see file CONTACT for details)
Multimediale Systeme der Informationsverarbeitung
Institut fuer Informatik
Christian-Albrechts-Universitaet Kiel
BIAS is free software; you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 2.1 of the License, or
(at your option) any later version.
BIAS is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with BIAS; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
/**
@example ExampleRandom.cpp
@relates Random
@brief Example for random number generator
@ingroup g_examples
@author MIP
*/
// must be first:
//#include <Base/Common/LeakChecking.h>
#include <cmath>
#include <cstring>
#include <Base/Math/Random.hh>
#include <Base/Debug/TimeMeasure.hh>
using namespace BIAS;
using namespace std;
#define HIST_BIN_COUNT 64
int main()
{
Random ran;
double num;
unsigned int hist[HIST_BIN_COUNT];
//, histn[HIST_BIN_COUNT];
//unsigned int histnum=0;
double min=-100.0, max=100.0;
//double mean=0.0, sigma=1.0;
//long unsigned int outside=0;
long unsigned int count=100000;
long unsigned int sumuniform=0;
//, sumnormal=0;
//double sigma_int=2.576;
//long int numint=0;
memset((void *)hist, 0, HIST_BIN_COUNT*sizeof(unsigned int));
TimeMeasure timer;
double *uniform=new double[count];
timer.Start();
ran.GetUniformDistributed(min, max, count, uniform);
timer.Stop();
cerr << "getting "<<count<<" uniform distributed random vars took "
<< timer.GetRealTime() << " us"<< endl;
for (register unsigned int i=0; i<count; i++){
num = uniform[i];
// num = ran.GetUniformDistributed(min, max);
//cerr << num << endl;
hist[(unsigned int)floor((num-min)/(max-min)*HIST_BIN_COUNT)]++;
}
for (int i=0; i< HIST_BIN_COUNT; i++){
cout << i << ": \t" << hist[i] << endl;
sumuniform+=hist[i];
}
cerr << "got "<<sumuniform<<" uniform distributed vars\n";
sumuniform=0;
delete[] uniform;
return 0;
//
// memset((void *)hist, 0, HIST_BIN_COUNT*sizeof(unsigned int));
// memset((void *)histn, 0, HIST_BIN_COUNT*sizeof(unsigned int));
//
// for (register unsigned int i=0; i<count; i++){
// numint=ran.GetUniformDistributedInt(0, HIST_BIN_COUNT-1);
// hist[numint]++;
// }
// cout << "uniform distributed integer between 0 and HIST_BIN_COUNT" << endl;
// for (int i=0; i< HIST_BIN_COUNT; i++){
// cout << i << ": \t" << hist[i] << endl;
// sumuniform+=hist[i];
// }
// cout << " got "<<sumuniform
// << " uniform distributed integer random values between 0 "
// << "and HIST_BIN_COUNT-1"<<endl;
// sumuniform=0;
//
//
// memset((void *)hist, 0, HIST_BIN_COUNT*sizeof(unsigned int));
//
// for (register unsigned int i=0; i<count; i++){
// num = ran.GetUniformDistributed(min, max);
// hist[(unsigned int)floor((num-min)/(max-min)*HIST_BIN_COUNT)]++;
// num = ran.GetNormalDistributed(mean, sigma);
// // 95.4% of all values lie within mean+-2*sigma
// histnum =
// (unsigned int)floor((num - mean + sigma_int*sigma)/
// (2*sigma_int*sigma)*HIST_BIN_COUNT);
// if ((histnum<0) || (histnum >=HIST_BIN_COUNT))
// outside++;
// else
// histn[histnum]++;
// }
// cout << "bin\tuniform \tnormal" << endl;
// for (int i=0; i< HIST_BIN_COUNT; i++){
// cout << i << ": \t" << hist[i] << " \t" << histn[i] << endl;
// sumuniform+=hist[i];
// sumnormal+=histn[i];
// }
// cout << "outside of choosen " << sigma_int
// << " sigma interval : #" << outside
// << " = " << (double)outside/(double)count*100.0 << "%" << endl;
//// cout << "normal distributed: " << endl;
//// for (int i=0; i< HIST_BIN_COUNT; i++){
//// cout << i << ": \t" << histn[i] << endl;
//// }
// cout << " got "<<sumuniform
// << " uniform distributed double random values between 0 "
// << "and HIST_BIN_COUNT-1"<<endl;
//
// cout << " got "<<sumnormal<<" normal distributed random values"<<endl;
//
// delete[] uniform;
// return 0;
}