Write a function that generates one of 3 numbers according to given probabilities
2014-06-05 19:50
537 查看
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%..
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%..
// 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; }
相关文章推荐
- Given n pairs of parentheses, write a function to generate all combinations of well-formed parenthe
- 1.3 Given two strings, write a method to decide if one is a permutation of the other.
- Given two strings s and t, write a function to determine if t is an anagram of s.
- Given an integer, write a function to determine if it is a power of two
- Given an integer, return all sequences of numbers that sum to it
- Given two strings s and t, write a function to determine if t is an anagram of s.
- Write one line C function to find whether a no is power of two
- Given an array nums, write a function to move all 0's to the end of it while maintaining the relativ
- Given constant integers x and t, write a function that takes no argument and returns true if the function has been called x number of times in last t secs.
- Given an array of integers, return indices of the two numbers such that they add up to a specific ta
- Given the string of parentheses only, write the function to check if they are balanced. ((())) is ba
- leetCode Given an integer (signed 32 bits), write a function to check whether it is a power of 4
- Given two binary trees, write a function to check if they are equal or not.
- Write a C program to print all permutations of a given string
- How to limit an array of similar hashes to those that have more than one of the same key:value pair
- Data Structure Linked List: Write a function to get the intersection point of two Linked Lists.
- how to print the spend time of one function
- Write a program to copy its input to its output, replacing each string of one or more blanks by a single blank.
- All-In-One Code Framework(AIO): 如何使用C#编写进程外的COM组件 (How to write an out-of-proc COM server in C#)
- No resource found that matches the given name (at 'layout_toLeftOf' with value '@id/ history_child_