samedi 14 septembre 2019

C++ random number generator only generates 0 - C++ 11

I am trying to generate a random number between 0 and 2 (inclusive) for a C++ program:

#include <iostream>
#include <string>
#include <cstdlib>
#include <random>

using namespace std;

    int generateRowMovement(){
        //http://www.cplusplus.com/reference/random/uniform_int_distribution/
        default_random_engine generator;
        uniform_int_distribution<int> distribution(0,2);
        int row = distribution(generator);
        return row;
    }

int main(){
    int row = generateRowMovement();
    cout << row << endl;

    int row2 = generateRowMovement();
    cout << row2 << endl;

    int row3 = generateRowMovement();
    cout << row3 << endl;

    return 0;
}

All 3 results are:

0
0
0

I have been testing this for 30 minute and it is always zero. I have tried following this link and this SO post (which posts link back to), but none of their solution is fix this problem. How can I generate 0, 1, and 2?




Aucun commentaire:

Enregistrer un commentaire