2

I have a variable cnt, whose value is checked via an if/else statement as follows:
If cnt<=2, then call func
Else if cnt > 2, then call func with the probability of P=3/(2*cnt).
How can I implement this probability-based execution of code in C++?

void func() {
    sendMsg();
}
Ciro Santilli OurBigBook.com
  • 347,512
  • 102
  • 1,199
  • 985
Mehdi Haghgoo
  • 3,144
  • 7
  • 46
  • 91

2 Answers2

6

Use something like std::uniform_real:

#include <random>

std::random_device rd;
std::mt19937 gen(rd());
std::uniform_real_distribution<> dis(0, 1);

void func() {
    // Probability 0.3
    if(dis(gen) < 0.3)
        sendMsg();
}
Ami Tavory
  • 74,578
  • 11
  • 141
  • 185
  • Are these classes for C++? My compiler does not seem to understand std::random_device and std::uniform_real_distribution. – Mehdi Haghgoo Jan 22 '16 at 10:04
  • You should find out how to get your compiler to use C++11 (which is the current mainstream version). Which compiler is it? – Ami Tavory Jan 22 '16 at 10:13
  • actually, I am typing just make to build the project after modification, but `gcc --version` reports `gcc (GCC) 5.1.1 20150618 (Red Hat 5.1.1-4)` – Mehdi Haghgoo Jan 22 '16 at 10:42
  • @JasonStack OK, so try adding `--std=c++11` to your gcc command line invocation. – Ami Tavory Jan 22 '16 at 11:20
  • Thank you Ami. I am modifying file aodv.cc of ns-2 c++ code and have to use `make` to build it. I added `CXXFLAGS=-g -std=c++11 -Wall -pedantic` as given in [this post](http://stackoverflow.com/questions/16886591/how-do-i-enable-c11-in-gcc) to [Makefile.in](http://nsnam.cvs.sourceforge.net/viewvc/nsnam/ns-2/Makefile.in?revision=1.321&view=markup) in the project directory, but it doesn't work. – Mehdi Haghgoo Jan 22 '16 at 12:03
  • @JasonStack Pretty sure it should be `--std=c++11` (note two `-` chars). Just tried building it by me, and it built fine. If it doesn't build for you, I suggest you start a separate question about getting contemporary C++ (C++11) working with your build setting. – Ami Tavory Jan 22 '16 at 12:08
2

std::map::lower_bound + partial_sum + C++11 lambdas

Here is a generalization where you can easily give a probability for each lambda.

#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <random>
#include <vector>

int main() {
    typedef std::pair<double, std::function<void()>> P;
    auto v = std::vector<P> {
        {0.2, [](){std::cout << "a" << std::endl;}},
        {0.5, [](){std::cout << "b" << std::endl;}},
        {0.3, [](){std::cout << "c" << std::endl;}}
    };
    std::partial_sum(v.begin(), v.end(), v.begin(),
        [](const P& x, const P& y){return P(x.first + y.first, y.second);}
    );
    auto m = std::map<P::first_type, P::second_type>(v.begin(), v.end());
    std::random_device r;
    std::mt19937 prng(r());
    std::uniform_real_distribution<> dist(0.0, 1.0);
    for (auto i = 0u; i < 10u; ++i) {
        auto r = dist(prng);
        std::cout << r << std::endl;
        m.lower_bound(r)->second();
        std::cout << std::endl;
    }
}

Sample output:

0.255392
b

0.0884198
a

0.8279
c

0.095513
a

0.34819
b

0.20279
b

0.327021
b

0.402535
b

0.64069
b

0.848767
c

Related: How do I select a range of values in a switch statement?

Community
  • 1
  • 1
Ciro Santilli OurBigBook.com
  • 347,512
  • 102
  • 1,199
  • 985