Skip to content
Related Articles

Related Articles

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

View Discussion
Improve Article
Save Article
  • Difficulty Level : Easy
  • Last Updated : 14 Jan, 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.
 

C




// This function generates 'x' with probability px/100, 'y' with
// probability py/100  and 'z' with probability pz/100:
// Assumption: px + py + pz = 100 where px, py and pz lie
// between 0 to 100
int random(int x, int y, int z, int px, int py, int pz)
{      
        // Generate a number from 1 to 100
        int r = rand(1, 100);
      
        // r is smaller than px with probability px/100
        if (r <= px)
            return x;
 
         // r is greater than px and smaller than or equal to px+py
         // with probability py/100
        if (r <= (px+py))
            return y;
 
         // r is greater than px+py and smaller than or equal to 100
         // with probability pz/100
        else
            return z;
}

Java




// This function generates 'x' with probability px/100, 'y'
// with probability py/100  and 'z' with probability pz/100:
// Assumption: px + py + pz = 100 where px, py and pz lie
// between 0 to 100
static int random(int x, int y, int z, int px, int py,
                  int pz)
{
    // Generate a number from 1 to 100
    int r = (int)(Math.random() * 100);
 
    // r is smaller than px with probability px/100
    if (r <= px)
        return x;
 
    // r is greater than px and smaller than or equal to
    // px+py with probability py/100
    if (r <= (px + py))
        return y;
 
    // r is greater than px+py and smaller than or equal to
    // 100 with probability pz/100
    else
        return z;
}
 
// This code is contributed by subhammahato348.

Python3




import random
 
# This function generates 'x' with probability px/100, 'y' with
# probability py/100  and 'z' with probability pz/100:
# Assumption: px + py + pz = 100 where px, py and pz lie
# between 0 to 100
def random(x, y, z, px, py, pz): 
     
    # Generate a number from 1 to 100
    r = random.randint(1, 100)
     
    #  r is smaller than px with probability px/100
    if (r <= px):
        return x
     
    # r is greater than px and smaller than
    # or equal to px+py with probability py/100
    if (r <= (px+py)):
        return y
         
    # r is greater than px+py and smaller than
    # or equal to 100 with probability pz/100
    else:
        return z
     
# This code is contributed by rohan07

C#




// This function generates 'x' with probability px/100, 'y'
// with probability py/100  and 'z' with probability pz/100:
// Assumption: px + py + pz = 100 where px, py and pz lie
// between 0 to 100
static int random(int x, int y, int z, int px, int py,
                  int pz)
{
    // Generate a number from 1 to 100
    Random rInt = new Random();
    int r = rInt.Next(0, 100);
 
    // r is smaller than px with probability px/100
    if (r <= px)
        return x;
 
    // r is greater than px and smaller than or equal to
    // px+py with probability py/100
    if (r <= (px + py))
        return y;
 
    // r is greater than px+py and smaller than or equal to
    // 100 with probability pz/100
    else
        return z;
}
 
// This code is contributed by subhammahato348.

Javascript




// This function generates 'x' with probability px/100, 'y' with
// probability py/100  and 'z' with probability pz/100:
// Assumption: px + py + pz = 100 where px, py and pz lie
// between 0 to 100
function random(x, y, z, px, py, pz)
{      
        // Generate a number from 1 to 100
        let r = Math.floor(Math.random() * 100) + 1;
      
        // r is smaller than px with probability px/100
        if (r <= px)
            return x;
 
         // r is greater than px and smaller than or equal to px+py
         // with probability py/100
        if (r <= (px+py))
            return y;
 
         // r is greater than px+py and smaller than or equal to 100
         // with probability pz/100
        else
            return z;
}
 
// This code is contributed by subhammahato348.

Time Complexity: O(1)

Auxiliary Space: O(1)

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
 


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!