Basic Image AlgorithmS Library  2.8.0
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
ExampleRandom.cpp
1 /*
2 This file is part of the BIAS library (Basic ImageAlgorithmS).
3 
4 Copyright (C) 2003-2009 (see file CONTACT for details)
5  Multimediale Systeme der Informationsverarbeitung
6  Institut fuer Informatik
7  Christian-Albrechts-Universitaet Kiel
8 
9 
10 BIAS is free software; you can redistribute it and/or modify
11 it under the terms of the GNU Lesser General Public License as published by
12 the Free Software Foundation; either version 2.1 of the License, or
13 (at your option) any later version.
14 
15 BIAS is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU Lesser General Public License for more details.
19 
20 You should have received a copy of the GNU Lesser General Public License
21 along with BIAS; if not, write to the Free Software
22 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 */
24 
25 /**
26  @example ExampleRandom.cpp
27  @relates Random
28  @brief Example for random number generator
29  @ingroup g_examples
30  @author MIP
31 */
32 // must be first:
33 //#include <Base/Common/LeakChecking.h>
34 
35 
36 #include <cmath>
37 #include <cstring>
38 #include <Base/Math/Random.hh>
39 #include <Base/Debug/TimeMeasure.hh>
40 
41 using namespace BIAS;
42 using namespace std;
43 
44 #define HIST_BIN_COUNT 64
45 
46 int main()
47 {
48  Random ran;
49  double num;
50  unsigned int hist[HIST_BIN_COUNT];
51  //, histn[HIST_BIN_COUNT];
52  //unsigned int histnum=0;
53  double min=-100.0, max=100.0;
54  //double mean=0.0, sigma=1.0;
55  //long unsigned int outside=0;
56  long unsigned int count=100000;
57  long unsigned int sumuniform=0;
58  //, sumnormal=0;
59  //double sigma_int=2.576;
60  //long int numint=0;
61 
62  memset((void *)hist, 0, HIST_BIN_COUNT*sizeof(unsigned int));
63 
64  TimeMeasure timer;
65  double *uniform=new double[count];
66  timer.Start();
67  ran.GetUniformDistributed(min, max, count, uniform);
68  timer.Stop();
69  cerr << "getting "<<count<<" uniform distributed random vars took "
70  << timer.GetRealTime() << " us"<< endl;
71  for (register unsigned int i=0; i<count; i++){
72  num = uniform[i];
73  // num = ran.GetUniformDistributed(min, max);
74  //cerr << num << endl;
75  hist[(unsigned int)floor((num-min)/(max-min)*HIST_BIN_COUNT)]++;
76  }
77  for (int i=0; i< HIST_BIN_COUNT; i++){
78  cout << i << ": \t" << hist[i] << endl;
79  sumuniform+=hist[i];
80  }
81  cerr << "got "<<sumuniform<<" uniform distributed vars\n";
82  sumuniform=0;
83 
84  delete[] uniform;
85 
86  return 0;
87 
88 
89 //
90 // memset((void *)hist, 0, HIST_BIN_COUNT*sizeof(unsigned int));
91 // memset((void *)histn, 0, HIST_BIN_COUNT*sizeof(unsigned int));
92 //
93 // for (register unsigned int i=0; i<count; i++){
94 // numint=ran.GetUniformDistributedInt(0, HIST_BIN_COUNT-1);
95 // hist[numint]++;
96 // }
97 // cout << "uniform distributed integer between 0 and HIST_BIN_COUNT" << endl;
98 // for (int i=0; i< HIST_BIN_COUNT; i++){
99 // cout << i << ": \t" << hist[i] << endl;
100 // sumuniform+=hist[i];
101 // }
102 // cout << " got "<<sumuniform
103 // << " uniform distributed integer random values between 0 "
104 // << "and HIST_BIN_COUNT-1"<<endl;
105 // sumuniform=0;
106 //
107 //
108 // memset((void *)hist, 0, HIST_BIN_COUNT*sizeof(unsigned int));
109 //
110 // for (register unsigned int i=0; i<count; i++){
111 // num = ran.GetUniformDistributed(min, max);
112 // hist[(unsigned int)floor((num-min)/(max-min)*HIST_BIN_COUNT)]++;
113 // num = ran.GetNormalDistributed(mean, sigma);
114 // // 95.4% of all values lie within mean+-2*sigma
115 // histnum =
116 // (unsigned int)floor((num - mean + sigma_int*sigma)/
117 // (2*sigma_int*sigma)*HIST_BIN_COUNT);
118 // if ((histnum<0) || (histnum >=HIST_BIN_COUNT))
119 // outside++;
120 // else
121 // histn[histnum]++;
122 // }
123 // cout << "bin\tuniform \tnormal" << endl;
124 // for (int i=0; i< HIST_BIN_COUNT; i++){
125 // cout << i << ": \t" << hist[i] << " \t" << histn[i] << endl;
126 // sumuniform+=hist[i];
127 // sumnormal+=histn[i];
128 // }
129 // cout << "outside of choosen " << sigma_int
130 // << " sigma interval : #" << outside
131 // << " = " << (double)outside/(double)count*100.0 << "%" << endl;
132 //// cout << "normal distributed: " << endl;
133 //// for (int i=0; i< HIST_BIN_COUNT; i++){
134 //// cout << i << ": \t" << histn[i] << endl;
135 //// }
136 // cout << " got "<<sumuniform
137 // << " uniform distributed double random values between 0 "
138 // << "and HIST_BIN_COUNT-1"<<endl;
139 //
140 // cout << " got "<<sumnormal<<" normal distributed random values"<<endl;
141 //
142 // delete[] uniform;
143 // return 0;
144 }
double GetUniformDistributed(const double min, const double max)
on succesive calls return uniform distributed random variable between min and max ...
Definition: Random.hh:84
double GetRealTime() const
return real time (=wall time clock) in usec JW For Win32: real-time is measured differently from user...
class for producing random numbers from different distributions
Definition: Random.hh:51
class TimeMeasure contains functions for timing real time and cpu time.
Definition: TimeMeasure.hh:111