Select the target Java Algorithm by weight

Source: Internet
Author: User

Recently, a military commanders system is a complicated system with many use cases. I have arranged for two weeks for development, but it will take one week for development alone, and the rest of the time will be used for debugging. For this system, I am more interested in the weight-related part. Each military will have a different probability of appearance, and each military will have multiple growth values with different weights. Here there are two aspects related to the weight. At first I thought I had written a very clever algorithm, and now I think it is so inefficient. Because the number of generals is uncertain and the sum of weights is uncertain, it is a bit tricky at the beginning. The solution is yes, but it does not feel good enough.

Assuming that the sum of weights is total, a random number is generated in this range. Observe the intervals of the random number to determine the weight range.

For example, there are three military commanders A, B, and C. Their probability of appearance is 30%, 40%, and 30%, respectively. First, a random number is generated. The total weight here is 100, which is divided into three intervals: 1 ~ 30, 31 ~ 70, 71 ~ 100. The range of natural random numbers is also less than 100. Suppose the random number is 49, obviously 49 is in 31 ~ Within the range of 70, it can be determined that the random generate will be B. The idea is like this, but how can we implement it using algorithms?

The simplest method for determining a situation is as follows:

 
  1. Int rand = 49; // random number, which is assumed to be a given value.
  2. Int A = 30;
  3. Int B = 40;
  4. Int C = 30;
  5. If (rand> 0 & rand <= ){
  6. Return;
  7. }
  8. Else if (rand> A & rand <= A + B ){
  9. Return B;
  10. }
  11. Else if (rand> A + B & rand <= A + B + C ){
  12. Return C;
  13. }

But as mentioned above, the sum of the weights is uncertain, and the number of military commanders is uncertain.

Then I came up with a simple method:

 
  1. Int rand = 49; // Random Number
  2. Int sum = 0;
  3. List <> list = new ArrayList <> (); // assume this is a List of military commanders.
  4. For (int I = 0; I <list. size (); I ++ ){
  5. Sum = list. get (I). getWeight (); // the probability of occurrence of the military commander.
  6. If (rand <= sum ){
  7. Return I; // return the index corresponding to the attach
  8. }
  9. }

I don't know if this is a good solution? Or what's wrong?

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.