How to simulate rolling two dice (game cubes) with values 1–6 in C++

1 Answer

0 votes
#include <iostream>
#include <random>

// Function that returns a random number from 1 to 6
int rollDice() {
    static std::random_device rd;
    static std::mt19937 gen(rd());
    static std::uniform_int_distribution<int> dist(1, 6);

    return dist(gen);
}

int main() {
    int dice1 = rollDice();
    int dice2 = rollDice();

    std::cout << "Dice 1: " << dice1 << "\n";
    std::cout << "Dice 2: " << dice2 << "\n";
}

  
  
/*
run:

Dice 1: 2
Dice 2: 3
  
*/

 



answered Feb 18 by avibootz
edited Feb 18 by avibootz

Related questions

...