You are given a function rand(a, b) which generates equiprobable random numbers between [a, b] inclusive. Generate 3 numbers x, y, z with probability P(x), P(y), P(z) such that P(x) + P(y) + P(z) = 1 using the given rand(a,b) function.
The idea is to utilize the equiprobable feature of the rand(a,b) provided. Let the given probabilities be in percentage form, for example P(x)=40%, P(y)=25%, P(z)=35%..
Following are the detailed steps.
1) Generate a random number between 1 and 100. Since they are equiprobable, the probability of each number appearing is 1/100.
2) Following are some important points to note about generated random number ‘r’.
a) ‘r’ is smaller than or equal to P(x) with probability P(x)/100.
b) ‘r’ is greater than P(x) and smaller than or equal P(x) + P(y) with P(y)/100.
c) ‘r’ is greater than P(x) + P(y) and smaller than or equal 100 (or P(x) + P(y) + P(z)) with probability P(z)/100.
This function will solve the purpose of generating 3 numbers with given three probabilities.
This article is contributed by Harsh Agarwal. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above
Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.
- Find probability that a player wins when probabilities of hitting the target are given
- Probability of A winning the match when individual probabilities of hitting the target given
- Count of ways to write N as a sum of three numbers
- Write a program to add two numbers in base 14
- Write an iterative O(Log y) function for pow(x, y)
- Writing power function for large numbers
- Find Maximum and Minimum of two numbers using Absolute function
- Euler's Totient function for all numbers smaller than or equal to n
- Write a program to calculate pow(x,n)
- Ways to write N as sum of two or more positive integers | Set-2
- Write a program to reverse digits of a number
- Write a program to print all permutations of a given string
- Write you own Power without using multiplication(*) and division(/) operators
- Write an Efficient Method to Check if a Number is Multiple of 3
- Write an Efficient C Program to Reverse Bits of a Number
- Fill the missing numbers in the array of N natural numbers such that arr[i] not equal to i
- Check if a given pair of Numbers are Betrothed numbers or not
- Numbers less than N which are product of exactly two distinct prime numbers
- Print N lines of 4 numbers such that every pair among 4 numbers has a GCD K
- Maximum sum of distinct numbers such that LCM of these numbers is N