Advertisement
einWikinger

Weighted Random Number Generator algorithm

Jul 6th, 2011
667
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.08 KB | None | 0 0
  1. // WeightedPRNG.cpp : Definiert den Einstiegspunkt für die Konsolenanwendung.
  2. //
  3.  
  4. #include "stdafx.h"
  5. #include <vector>
  6. #include <iostream>
  7. #include <iomanip>
  8.  
  9. #define USE_CUSTOM_RNG
  10.  
  11. #ifdef USE_CUSTOM_RNG
  12. #include "RandomNumberGenerator.h"
  13. #include <windows.h>
  14. #endif
  15.  
  16. typedef long double Number;
  17.  
  18. Number random_unit()
  19. {
  20. #ifdef USE_CUSTOM_RNG
  21.     // this actually wraps around a boost Mersenne Twister PRNG implementation
  22.     // which will give better distributed numbers
  23.     // see http://www.boost.org/doc/libs/1_46_1/doc/html/boost_random.html
  24.     static RandomNumberGenerator rng( (size_t)GetTickCount64() );
  25.  
  26.     return rng.random_double_unit();
  27. #else
  28.     // pretty bad resolution (16bit)
  29.     // bad distribution pattern
  30.     return rand() / (Number)RAND_MAX;
  31. #endif
  32. }
  33.  
  34. struct tWeightedItem
  35. {
  36.     tWeightedItem( const std::string& name, Number weight )
  37.         : choses(0), name(name), weight(weight){};
  38.  
  39.     std::string name;
  40.     Number weight;
  41.     Number rel_weight;
  42.     int choses;
  43. };
  44.  
  45. typedef std::vector<tWeightedItem> WeightedItemList;
  46.  
  47. /// Actual weighted random choosing algorithm implementation
  48. /// Works like cutting all items from paper with lengths
  49. /// equalling their weight, then aligning them and throwing
  50. /// a random dart at it:
  51. ///                                   v Dart
  52. ///  Strip  |-------|----|---------|---x-|------|--------|
  53. ///  Item#      #0     #1     #2      #3    #4       #5
  54. /// Weight      8      5      10      6     7        9
  55. WeightedItemList::iterator choose( WeightedItemList& wil )
  56. {
  57.     Number weight_sum = 0.0f;
  58.  
  59.     // sum up weights
  60.     for( size_t n=0; n<wil.size(); n++ )
  61.         weight_sum += wil[n].weight;
  62.  
  63.     // probe a random point within our total strip length,
  64.     // random_double_unit will return a number in [0,1] (closed interval)
  65.     Number random_choice = random_unit() * weight_sum;
  66.  
  67.     Number sum = 0.0f;
  68.  
  69.     // The loop will find the item where the probe number hits the
  70.     // range of the weight
  71.     for( WeightedItemList::iterator it=wil.begin(); it != wil.end(); ++it )
  72.     {
  73.         const tWeightedItem& wc = *it;
  74.  
  75.         // I'm not sure about the equality operators here, because some
  76.         // numbers fall into two categories. But as the PRNG numbers are
  77.         // inside [0,1] I have to include the leftmost and rightmost borders
  78.         // of the number range which only works with this operator combination.
  79.         // If your PRNG spits out numbers in (0,1] or [0,1) you might want
  80.         // to adjust the condition.
  81.         if( random_choice >= sum && random_choice <= (sum + wc.weight) )
  82.             return it;
  83.  
  84.         sum += wc.weight;
  85.     }
  86.  
  87.     // should never happen, if it does, the
  88.     // random probe will
  89.     return wil.end();
  90. }
  91.  
  92. // Algorithm test function, checks if the weights assigned equal the
  93. // actual stochastic occurences
  94. int main(int argc, char* argv[])
  95. {
  96.     // number of stochastic samples to take
  97.     // the higher, the more should the actual distribution
  98.     // converge to the intended weight
  99.     const size_t SAMPLES = 10000000;
  100.  
  101.     WeightedItemList items;
  102.  
  103.     // Some test probands
  104.     items.push_back( tWeightedItem( "Hans", 1));
  105.     items.push_back( tWeightedItem( "Frank", 7));
  106.     items.push_back( tWeightedItem( "Joe", 2));
  107.     items.push_back( tWeightedItem( "Lisa", 5));
  108.     items.push_back( tWeightedItem( "Othello", 10));
  109.  
  110.     Number total = 0.0f;
  111.  
  112.     for( size_t n=0; n<items.size(); n++ )
  113.     {
  114.         total += items[n].weight;
  115.     }
  116.  
  117.     for( size_t n=0; n<items.size(); n++ )
  118.     {
  119.         items[n].rel_weight = items[n].weight / total;
  120.     }
  121.  
  122.     int fails = 0;
  123.  
  124.     for( size_t n=0; n<SAMPLES; n++ )
  125.     {
  126.         WeightedItemList::iterator chosen = choose( items );
  127.  
  128.         if( chosen != items.end() )
  129.             chosen->choses++;
  130.         else
  131.             fails++;
  132.     }
  133.  
  134.     std::cout << std::setw(15) << "Item" <<
  135.         " | " << std::setprecision(5) << std::setw(10) << std::fixed << "Expected" <<
  136.         " | " << std::setprecision(5) << std::setw(10) << std::fixed << "Real" <<
  137.         " | " << std::setprecision(5) << std::setw(10) << std::fixed << "Difference" << std::endl;
  138.  
  139.     Number sum_expected = 0;
  140.     Number sum_real = 0;
  141.     Number sum_differences = 0;
  142.  
  143.     for( size_t n=0; n<items.size(); n++ )
  144.     {
  145.         tWeightedItem& wi = items[n];
  146.  
  147.         Number possibility = (wi.choses / (Number)SAMPLES);
  148.         Number difference = fabs(possibility - wi.rel_weight);
  149.  
  150.         std::cout << std::setw(15) << wi.name.c_str() <<
  151.             " | " << std::setprecision(5) << std::setw(10) << std::fixed << wi.rel_weight <<
  152.             " | " << std::setprecision(5) << std::setw(10) << std::fixed << possibility <<
  153.             " | " << std::setprecision(5) << std::setw(10) << std::fixed << difference;
  154.  
  155.         if( fabs(wi.rel_weight - (wi.choses / (Number)SAMPLES)) > 0.001f)
  156.         {
  157.             std::cout << " [!]";
  158.         }
  159.  
  160.         sum_expected += wi.rel_weight;
  161.         sum_real += possibility;
  162.         sum_differences += difference;
  163.  
  164.         std::cout << std::endl;
  165.     }
  166.  
  167.     std::cout << std::setw(15) << "Totals" <<
  168.         " | " << std::setprecision(5) << std::setw(10) << std::fixed << sum_expected <<
  169.         " | " << std::setprecision(5) << std::setw(10) << std::fixed << sum_real <<
  170.         " | " << std::setprecision(5) << std::setw(10) << std::fixed << sum_differences << std::endl;
  171.  
  172.     std::cout << "Samples: " << SAMPLES << std::endl << "Fails: " << fails << std::endl;
  173.  
  174.     return 0;
  175. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement