2
0

mRandomSet.h 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2012 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. #ifndef _MRANDOMSET_H_
  23. #define _MRANDOMSET_H_
  24. #ifndef _MRANDOM_H_
  25. #include "math/mRandom.h"
  26. #endif
  27. template <class T>
  28. class MRandomSet
  29. {
  30. protected:
  31. MRandomLCG *mRandGen;
  32. Vector<T> mItems;
  33. Vector<F32> mProbability;
  34. F32 mSum;
  35. public:
  36. MRandomSet( MRandomLCG *randGen = &gRandGen );
  37. void add( const T &item, F32 probability );
  38. /// Return a random item from the set using the specified per
  39. /// item probability distribution.
  40. T get();
  41. };
  42. template<class T>
  43. inline MRandomSet<T>::MRandomSet( MRandomLCG *randGen )
  44. : mRandGen( randGen ),
  45. mSum( 0.0f )
  46. {
  47. }
  48. template<class T>
  49. inline void MRandomSet<T>::add( const T &item, F32 probability )
  50. {
  51. AssertFatal( probability > 0.0f, "MRandomDeck - item probability must be positive." );
  52. mItems.push_back( item );
  53. mProbability.push_back( probability );
  54. mSum += probability;
  55. }
  56. template<class T>
  57. inline T MRandomSet<T>::get()
  58. {
  59. AssertFatal( mSum > 0.0f, "MRandomDeck - no items to get." );
  60. F32 rand = mRandGen->randF(0.0f, mSum);
  61. F32 prev = -1.0f;
  62. F32 curr = 0.0f;
  63. for ( S32 i = 0; i < mItems.size(); i++ )
  64. {
  65. curr += mProbability[i];
  66. if ( rand > prev && rand <= curr )
  67. return mItems[i];
  68. prev = curr;
  69. }
  70. AssertFatal( false, "MRandomSet::get() has failed." );
  71. return NULL;
  72. }
  73. #endif //_MRANDOMSET_H_