VOOZH about

URL: https://www.geeksforgeeks.org/dsa/write-a-function-to-generate-3-numbers-according-to-given-probabilities/

⇱ Write a function that generates one of 3 numbers according to given probabilities - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Write a function that generates one of 3 numbers according to given probabilities

Last Updated : 12 Aug, 2022

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.
 

Time Complexity: O(1)

Auxiliary Space: O(1)

This function will solve the purpose of generating 3 numbers with given three probabilities.
 

Comment
Article Tags: