in Education by
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers. Want to improve this question? Update the question so it's on-topic for Stack Overflow. Closed 4 years ago. Improve this question I am doing a project at the moment, and in the interest of code reuse, I went looking for a library that can perform some probabilistic accept/reject of an item: i.e., there are three people (a, b c), and each of them have a probability P{i} of getting an item, where p{a} denotes the probability of a. These probabilities are calculated at run time, and cannot be hardcoded. What I wanted to do is to generate one random number (for an item), and calculate who gets that item based on their probability of getting it. The alias method (http://books.google.com/books?pg=PA133&dq=alias+method+walker&ei=D4ORR8ncFYuWtgOslpVE&sig=TjEThBUa4odbGJmjyF4daF1AKF4&id=ERSSDBDcYOIC&output=html) outlined here explained how, but I wanted to see if there is a ready made implementation so I wouldn't have to write it up. JavaScript questions and answers, JavaScript questions pdf, JavaScript question bank, JavaScript questions and answers pdf, mcq on JavaScript pdf, JavaScript questions and solutions, JavaScript mcq Test , Interview JavaScript questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)

1 Answer

0 votes
by
Would something like this do? Put all p{i}'s in the array, function will return an index to the person who gets the item. Executes in O(n). public int selectPerson(float[] probabilies, Random r) { float t = r.nextFloat(); float p = 0.0f; for (int i = 0; i < probabilies.length; i++) { p += probabilies[i]; if (t < p) { return i; } } // We should not end up here if probabilities are normalized properly (sum up to one) return probabilies.length - 1; } EDIT: I haven't really tested this. My point was that the function you described is not very complicated (if I understood what you meant correctly, that is), and you shouldn't need to download a library to solve this.

Related questions

0 votes
    What will happen if we provide concrete implementation of method in interface? (a) The concrete class implementing ... of Java Select the correct answer from above options...
asked Feb 23, 2022 in Education by JackTerrance
0 votes
    What will happen if we provide concrete implementation of method in interface? Select the correct answer from above options...
asked Dec 21, 2021 in Education by JackTerrance
0 votes
    I have a c++11 type alias: using coord = std::array; Can I define the operator + for coord? ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 29, 2022 in Education by JackTerrance
0 votes
    I have a c++11 type alias: using coord = std::array; Can I define the operator + for coord? ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 26, 2022 in Education by JackTerrance
0 votes
    I have a c++11 type alias: using coord = std::array; Can I define the operator + for coord? ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 22, 2022 in Education by JackTerrance
0 votes
    ndarray is also known as the alias array. (a) True (b) False This question was posed to me in ... questions and answers pdf, Data Science interview questions for beginners...
asked Oct 28, 2021 in Education by JackTerrance
0 votes
    ndarray is also known as the alias array. (a) True (b) False...
asked Oct 7, 2021 in Technology by JackTerrance
0 votes
0 votes
    When we realize a specific implementation of a pancake algorithm, every move when we find the greatest of the ... b) Exponential functions c) Logarithmic functions d) Permutations...
asked Dec 29, 2022 in Technology by JackTerrance
0 votes
    Which of the following IS NOT one of the advantages associated with a robotics implementation program? (a) Low ... for worker fringe benefits Please answer the above question....
asked Oct 22, 2022 in Education by JackTerrance
0 votes
    Which of the following IS NOT one of the advantages associated with a robotics implementation program? (a) Low ... for worker fringe benefits Please answer the above question....
asked Oct 2, 2022 in Education by JackTerrance
0 votes
    Is Boltzman law practical for implementation? (a) yes (b) no The question was posed to me in a job ... Neural Networks of Neural Networks Please answer the above question....
asked Aug 27, 2022 in Education by JackTerrance
0 votes
    For practical implementation what type of approximation is used on boltzman law? (a) max field approximation (b) min ... d) none of the mentioned Please answer the above question....
asked Aug 27, 2022 in Education by JackTerrance
0 votes
    State the appropriate concept for the given statement. Application of the abstract concept of justice through the implementation of the law. Please answer the above question....
asked Aug 3, 2022 in Education by JackTerrance
...