HOME   ≫   SOFTWARE   ≫   USER GUIDE AND DOCUMENTATION   ≫   PRELIMINARY EXAMPLE 2


Software

User Guide and Documentation ‐ Preliminary Example 2

Stochastic sampling of 200 numbers in C++11 with Yotcopi

[ Download : second.cpp ]
#include <yotcopi.hpp>

int main(int argc, char** argv)
{
  using namespace yotcopi;
  using namespace yotcopi::shortkeys;

  auto rnd = random_real_01<>(time_seed());
  auto measurements = RealSimpleMeasurements();

  For(unsigned idx, urange(200))
    measurements << rnd();

  std::cout << "Mean  : " << measurements.mean() << "\n"
            << "Std   : " << measurements.std()  << "\n"
            << "Count : " << measurements.count() << "\n";

  return 0;
}
Compiling the Yotcopi code by
ymake second
and running the program
./second
the following output will be shown on the console.
Mean  : 0.526687
Std   : 0.297752
Count : 200

In the following, we explain the code further.

First, we declare a random number object rnd (of Yotcopi-defined type random_real_01) that generates a real number in the interval [0,1), with initial seed determined by the time at the start of simulation.
auto rnd = random_real_01<>(time_seed());
Second, we declare a measurements object measurements (of Yotcopi-defined type RealSimpleMeasurements) to deal with simple statistics handling.
auto measurements = RealSimpleMeasurements();
Third, we generate a random number, ie. rnd(), and dump it into the measurements object measurements
measurements << rnd();
repetitively in a loop of 200 repetitions.
For (unsigned idx, urange(200))
where urange(200) is a vector of unsigned integers [0, 1, 2, ..., 199].

Finally, we calculate the mean, std (standard deviation), and count:
measurements.mean()
measurements.std()
measurements.count()

More information can be found in the user guide. See more ≫
[Top]