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

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 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; ` `} ` |

*chevron_right*

*filter_none*

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

## Recommended Posts:

- Find probability that a player wins when probabilities of hitting the target are given
- 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
- 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 you own Power without using multiplication(*) and division(/) operators
- Write a program to reverse digits of a number
- Write a program to print all permutations of a given string
- Write an Efficient C Program to Reverse Bits of a Number
- Write an Efficient Method to Check if a Number is Multiple of 3
- 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
- Count numbers which can be constructed using two numbers