blob: 1dc068fcd64e44a6b61121792019d9a489aa4592 (
plain)
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
|
#include <iostream>
#include <cmath>
// Generate pseudo-random outcomes of tossing a biased coin 100 times.
// Compile with, e.g.
//
// g++ -o binom binom.cc && ./binom > binom.dat
const int trials=100000; // amount of data to collect
int flip(double wt=0.5) // 0 < wt < 1
{
if (rand() < (1-wt)*RAND_MAX)
return 0;
else
return 1;
}
void toss(unsigned int tosses, double wt=0.5)
{
int count=0;
for (int i=0; i < tosses; ++i)
count += flip(wt);
std::cout << count << "\t";
}
int main()
{
for (int j=0; j < trials; ++j)
{
toss(100, 0.05);
toss(100, 0.1);
toss(100, 0.3);
toss(100);
std::cout << "\n";
}
}
|