Friday 15 January 2010

algorithm - Randomly choosing from a list with weighted probabilities -


I have an array of N elements (representing N characters of a given alphabet), and each cell of the array To be an integer value, that integer value, which means that the number of incidents occurring in a given text of that letter Now I randomly choose a letter from all the letters of the alphabet, which is based on the number of obstacles given to them:

  • If the letter contains a positive (noorio ) Value, it can always be chosen by algorithm (certainly with big or small probability).

  • If letter A gets more value than B, then it should be more likely to be chosen by algorithms.

    Now, keeping that account in mind, I have come up with a simple algorithm that can work, but I was wondering what was a better job . It seems quite original, and I think that more clever things can be done to accomplish this more efficiently. This is the algorithm I thought:

    • Add all instances in the array. Store it in SUM
    • Choose random values ​​from 0 to SUM. Store it in RAN
    • [While] RAN & gt; 0, start from the beginning, go to each cell in the array (in order), and decrease the value of that cell with RAN
    • The last visit cell is selected

      So, is there any better work than this? Am I missing something?

      I know that modern computers can calculate this so fast I will not even know whether my algorithm is inefficient, so it is not practical, but more of a theoretical question.

      I prefer an explained algorithm instead of code for an answer, but if you feel more comfortable providing your answer in the code, then I have no problem with it.

      Thoughts:

      • All elements Repeat through and set the value of each
      • Generate a random number between the sum of 1 and all the frequencies
      • One for values ​​on this number (one above the first value) Or equal to the number).

        Example:

          element ABCD frequency 1 4 3 2 cumulative 1 5 8 10   

        Generate a random number in the category 1-10 (1 + 4 + 3 + 2 = 10, the last value in the cumulative list), perform a binary search, which will return the value as follows: < Pre> number element back 1a2b3b4b5b6c7c8c9d10d

No comments:

Post a Comment