Generate integer from 1 to 7 with equal probability using program in Python
Introduction: We usually write programs without even think about probability. Well, you can also write those programs by calculating the probability distribution function. This probability distribution function can be calculated with the help of given numbers and their frequencies. The below example shows this very well where we are using a quite technique to do this.
First of all, let us go through the coding part.
#an empty function which does nothing just return 0 def emp(): return 0 #A function that uses emp() function for generate number def main_fun(): j = 0 #j is the variable which keeps the number multiple of 5 it is just a refrence j = (3 * emp()) + (emp() - 3) # because 7*2 =14 so we are cheaking for everynumber that it should be below 21 if (j < 14): if(j < 0): return (j % 7 ) + 1 else: return (j % 7) + 1 return main_fun() print(main_fun());
Output: 5
Let us first understand how it is working,
Leave a Reply