mardi 30 août 2016

C++ binomial_distribution throws large number when type unsigned long?

When I type the binomial_distribution as an unsigned long it eventually throw a number that does not make sense although it seems legal to do so. Below is an example... What am I missing? Thanks in advance!

#include <iostream>
#include <stdlib.h>
#include <stdio.h>
#include <random>

using namespace std;

int main(int argc, const char * argv[])
{
    unsigned int    _RANDOM_SEED    = 1234;
    std::mt19937    _RANDOM_GENERATOR1(_RANDOM_SEED);

    unsigned long N = 3;
    double p = 0.02;
    unsigned long iter = 1000;

    // Distribution typed 'long'

    std::binomial_distribution<long> d(N, p);
    for(int i = 0; i < iter; i++) {
        unsigned long r = d(_RANDOM_GENERATOR1);
     cout << i << " " << r <<endl;
        if(r > N) {cout<<"PROBLEM"<<endl; exit(99);} // <-- never reached (which is good!)
    }

    // Distribution typed 'unsigned long'
    std::mt19937    _RANDOM_GENERATOR2(_RANDOM_SEED);

    std::binomial_distribution<unsigned long> d2(N, p);
    for(int i = 0; i < iter; i++) {
        unsigned long r = d2(_RANDOM_GENERATOR2);
        cout << i << " " << r <<endl;
        if(r > N) {cout<<"PROBLEM"<<endl; exit(99);} // <-- eventually reached, with r = huge number
    }
}

g++ --version Configured with: --prefix=/Applications/http://ift.tt/1d5DwEL --with-gxx-include-dir=/usr/include/c++/4.2.1 Apple LLVM version 6.0 (clang-600.0.57) (based on LLVM 3.5svn) Target: x86_64-apple-darwin13.4.0 Thread model: posix

Aucun commentaire:

Enregistrer un commentaire