#include<bits/stdc++.h>
using namespace std;

map<int, int> EventCounter(const vector<pair<int, int>>& EventsProb) {
    int totalProb = 100;  //Assuming that the Total Probability of All Events will be 1 (100 in Percentage)
    vector<pair<int, pair<int, int>>> Rangevector;
    int EventsRanges = 0;

    // Calculate probability ranges for each outcome
    for (const auto& outcome : EventsProb) {
        int outcomeValue = outcome.first;
        int prob = outcome.second;
        pair<int, int> probRange = make_pair(EventsRanges, EventsRanges + prob);
        Rangevector.push_back(make_pair(outcomeValue, probRange));
        EventsRanges += prob;
    }

    map<int, int> eventResults;
    int TotalTurns = 1000;

    // Initialize random number generator and distribution
    random_device rd;
    mt19937 generator(rd());
    uniform_int_distribution<> distribution(0, totalProb - 1);

    // Perform the simulation for a given number of trials (TotalTurns)
    // More the Probability of Event that means the More the Range the Event will get
    
    for (int i = 0; i < TotalTurns; ++i) {
        int randomNumber = distribution(generator); // Generate a random number
        for (const auto& outcomeRange : Rangevector) {
            int outcomeValue = outcomeRange.first;
            pair<int, int> probRange = outcomeRange.second;

            // Check if the random number falls within the current outcome's probability range
            if (probRange.first <= randomNumber && randomNumber < probRange.second) {
                eventResults[outcomeValue]++; // Increment the count for the selected Event or outcome
                break;
            }
        }
    }

    return eventResults;
}

int main() {
    vector<pair<int, int>> EventsProbDice = {{1, 10},{2, 30},{3, 15},{4, 15},{5, 30},{6, 0}};

    vector<pair<int, int>> EventsProbCoin = {{1, 35}, {2, 65}};

    // calling the function for the Events
    map<int, int> resultsDice = EventCounter(EventsProbDice);
    map<int, int> resultsCoin = EventCounter(EventsProbCoin);

    // Output the results
    cout << "Output for dice:" << endl;
    for (const auto& result : resultsDice) {
        cout << result.first << " appeared " << result.second << " times out of 1000 times" << endl;
    }

    cout << "\nOutput for coin:" << endl;
    for (const auto& result : resultsCoin) {
        cout << (result.first == 1 ? "Head" : "Tail") << " appeared " << result.second << " times out of 1000 times" << endl;
    }
    return 0;
}
 
by

C++ Online Compiler

Write, Run & Share C++ code online using OneCompiler's C++ online compiler for free. It's one of the robust, feature-rich online compilers for C++ language, running on the latest version 17. Getting started with the OneCompiler's C++ compiler is simple and pretty fast. The editor shows sample boilerplate code when you choose language as C++ and start coding!

Read inputs from stdin

OneCompiler's C++ online compiler supports stdin and users can give inputs to programs using the STDIN textbox under the I/O tab. Following is a sample program which takes name as input and print your name with hello.

#include <iostream>
#include <string>
using namespace std;

int main() 
{
    string name;
    cout << "Enter name:";
    getline (cin, name);
    cout << "Hello " << name;
    return 0;
}

About C++

C++ is a widely used middle-level programming language.

  • Supports different platforms like Windows, various Linux flavours, MacOS etc
  • C++ supports OOPS concepts like Inheritance, Polymorphism, Encapsulation and Abstraction.
  • Case-sensitive
  • C++ is a compiler based language
  • C++ supports structured programming language
  • C++ provides alot of inbuilt functions and also supports dynamic memory allocation.
  • Like C, C++ also allows you to play with memory using Pointers.

Syntax help

Loops

1. If-Else:

When ever you want to perform a set of operations based on a condition If-Else is used.

if(conditional-expression) {
   //code
}
else {
   //code
}

You can also use if-else for nested Ifs and If-Else-If ladder when multiple conditions are to be performed on a single variable.

2. Switch:

Switch is an alternative to If-Else-If ladder.

switch(conditional-expression){    
case value1:    
 // code    
 break;  // optional  
case value2:    
 // code    
 break;  // optional  
......    
    
default:     
 code to be executed when all the above cases are not matched;    
} 

3. For:

For loop is used to iterate a set of statements based on a condition.

for(Initialization; Condition; Increment/decrement){  
  //code  
} 

4. While:

While is also used to iterate a set of statements based on a condition. Usually while is preferred when number of iterations are not known in advance.

while (condition) {  
// code 
}  

5. Do-While:

Do-while is also used to iterate a set of statements based on a condition. It is mostly used when you need to execute the statements atleast once.

do {  
 // code 
} while (condition); 

Functions

Function is a sub-routine which contains set of statements. Usually functions are written when multiple calls are required to same set of statements which increases re-usuability and modularity. Function gets run only when it is called.

How to declare a Function:

return_type function_name(parameters);

How to call a Function:

function_name (parameters)

How to define a Function:

return_type function_name(parameters) {  
 // code
}