online compiler and debugger for c/c++

code. compile. run. debug. share.
Source Code   
Language
#include <iostream> #include <vector> #include <cstdlib> #include <ctime> struct Box { int goldBalls; int silverBalls; }; int main() { std::srand(std::time(0)); std::vector<Box> boxes = { {2, 0}, // Коробка с двумя золотыми {1, 1}, // Коробка с одним золотым и одним серебряным {0, 2} // Коробка с двумя серебряными }; const int N = 1000000; int successCount = 0; int validCases = 0; for (int i = 0; i < N; i++) { // 1. Выбираем случайную коробку int boxIndex = std::rand() % boxes.size(); Box box = boxes[boxIndex]; // 2. Случайно выбираем первый шар int totalBalls = box.goldBalls + box.silverBalls; int firstBallType = (std::rand() % totalBalls) < box.goldBalls ? 1 : 0; // 3. Если первый шар не золотой - пропускаем эксперимент if (firstBallType != 1) continue; validCases++; // 4. Убираем вытянутый шар из коробки if (box.goldBalls > 0) { box.goldBalls--; totalBalls--; } // 5. Вытягиваем второй шар if (totalBalls > 0) { int secondBallType = (std::rand() % totalBalls) < box.goldBalls ? 1 : 0; if (secondBallType == 1) { successCount++; } } } double probability = static_cast<double>(successCount) / validCases; std::cout << "Вероятность: " << probability << std::endl; return 0; }

Compiling Program...

Command line arguments:
Standard Input: Interactive Console Text
×

                

                

Program is not being debugged. Click "Debug" button to start program in debug mode.

#FunctionFile:Line
VariableValue
RegisterValue
ExpressionValue