- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

In this problem, we have to create a function that will generate three numbers based on the given probability.

For this, we will use the built-in random number generator function which is rand(a, b) which generates random numbers within the range [a, b] with equal probability.

Our task is to return only three numbers A, B, C which have the probability of occurrence as P(A), P(B), P(C) respectively and according to definition of probability P(A) + P(B) + P(C) = 1.

To create our function using rand(a,b). we will use its feature that the probability of occurrence of any number from a to b is the same. But we have to get A with probability P(A) and so on. So, for this, we will use percentage probabilities of the numbers i.e. if P(A) = 1/5, we will treat it as 20%.

So, the maximum probability percentage will be 100%, so we will generate a random number from 0 to 100 and based on the number generated we will return numbers between A, B, C based on the following conditions.

**Case 1** − If the number generated is between 0 and P(A), return A.

**Case 2** − If the number generated is between P(A) and P(A)+P(B), return B.

**Case 3** − If the number generated is between P(A)+P(B) and 1{P(A)+P(B)+P(C)}, return C.

Let’s see an example that will make the concept clear,

A = 3 , P(A) = 35% B = 43, P(B) = 50% C = 90, P(C) = 15%

The program will return,

3 if the rand function generates number between 0 and 35.

43 if the rand function generates number between 35 and 85.

90 if the rand function generates number between 85 and 100.

**Program to show the implementation of our logic,**

#include<iostream> using namespace std; int randomABC(int A, int B, int C, int PA, int PB, int PC){ int randNumber = rand()%100+1; if (randNumber <= PA) return A; if (randNumber <= (PA+PB)) return B; else return C; } int main(){ cout<<"Random number between 3, 45, 90 with given probabilities is : "<<randomABC(3, 43, 90, 35, 40, 25); return 0; }

Random number between 3, 45, 90 with given probabilities is : 3

- Related Questions & Answers
- JavaScript function that generates all possible combinations of a string
- Write a one line C function to round floating point numbers
- Create a MySQL stored procedure that generates five random numbers?
- Find probability that a player wins when probabilities of hitting the target are given in C++
- Write a function that counts the number of times a given int occurs in a Linked List in C++
- Write a program in C++ to remove duplicates from a given array of prime numbers
- Probability of A winning the match when individual probabilities of hitting the target given in C++
- How can I write a MySQL stored function that calculates the factorial of a given number?
- Sorting according to weights of numbers in JavaScript
- Sort the numbers according to their sum of digits in C++
- Design an unambiguous CFG in CNF that generates E?
- Write a Golang program to find prime numbers in a given range
- Write a program in Python to remove one or more than one columns in a given DataFrame
- Write a program in Python to perform average of rolling window size 3 calculation in a given dataframe
- Write a program in Python to filter armstrong numbers in a given series

Advertisements