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
- Generate 0 and 1 with 25% and 75% probability
- Converting Decimal Number lying between 1 to 3999 to Roman Numerals
- Select a Random Node from a Singly Linked List
- Birthday Paradox
- Replace all ‘0’ with ‘5’ in an input Integer
- K'th Smallest/Largest Element in Unsorted Array | Set 2 (Expected Linear Time)
- Find next greater number with same set of digits
- Count Possible Decodings of a given Digit Sequence
- Calculate the angle between hour hand and minute hand
- Find the smallest number whose digits multiply to a given number n
- Horner's Method for Polynomial Evaluation
- Program for nth Catalan Number
- How to check if a given number is Fibonacci number?
- Random number generator in arbitrary probability distribution fashion
- Generate integer from 1 to 7 with equal probability