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
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- 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 permutations of given array that generates the same Binary Search Tree (BST)
- Change the given string according to the given conditions
- Sort the numbers according to their product of digits
- Find the total marks obtained according to given marking scheme
- Decrypt the String according to given algorithm
- Minimize the sum of the array according the given condition
- Generate an array of size N according to the given rules
- Write a program to add two numbers in base 14
- Count of ways to write N as a sum of three numbers
- Sub-strings that start and end with one character and have at least one other
- Write a program to print all permutations of a given string
- Given two numbers as strings, find if one is a power of other
- Write an iterative O(Log y) function for pow(x, y)
- Count of groups having largest size while grouping according to sum of its digits
- Sort integers in array according to their distance from the element K
- Print numbers such that no two consecutive numbers are co-prime and every three consecutive numbers are co-prime
- Write a program to calculate pow(x,n)
- Write a program to reverse digits of a number