File: binom.cc

package info (click to toggle)
epix 1.2.22-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 3,256 kB
  • sloc: cpp: 16,847; sh: 5,054; makefile: 159; lisp: 6
file content (37 lines) | stat: -rw-r--r-- 672 bytes parent folder | download | duplicates (10)
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";
    }
}