PageRenderTime 70ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/indra/llcommon/llrand.h

https://bitbucket.org/lindenlab/viewer-beta/
C++ Header | 127 lines | 14 code | 13 blank | 100 comment | 0 complexity | 5624aa9ef25372be840508b2234e4519 MD5 | raw file
Possible License(s): LGPL-2.1
  1. /**
  2. * @file llrand.h
  3. * @brief Information, functions, and typedefs for randomness.
  4. *
  5. * $LicenseInfo:firstyear=2000&license=viewerlgpl$
  6. * Second Life Viewer Source Code
  7. * Copyright (C) 2010, Linden Research, Inc.
  8. *
  9. * This library is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU Lesser General Public
  11. * License as published by the Free Software Foundation;
  12. * version 2.1 of the License only.
  13. *
  14. * This library is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  17. * Lesser General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Lesser General Public
  20. * License along with this library; if not, write to the Free Software
  21. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  22. *
  23. * Linden Research, Inc., 945 Battery Street, San Francisco, CA 94111 USA
  24. * $/LicenseInfo$
  25. */
  26. #ifndef LL_LLRAND_H
  27. #define LL_LLRAND_H
  28. #include <boost/random/lagged_fibonacci.hpp>
  29. #include <boost/random/mersenne_twister.hpp>
  30. /**
  31. * Use the boost random number generators if you want a stateful
  32. * random numbers. If you want more random numbers, use the
  33. * c-functions since they will generate faster/better randomness
  34. * across the process.
  35. *
  36. * I tested some of the boost random engines, and picked a good double
  37. * generator and a good integer generator. I also took some timings
  38. * for them on linux using gcc 3.3.5. The harness also did some other
  39. * fairly trivial operations to try to limit compiler optimizations,
  40. * so these numbers are only good for relative comparisons.
  41. *
  42. * usec/inter algorithm
  43. * 0.21 boost::minstd_rand0
  44. * 0.039 boost:lagged_fibonacci19937
  45. * 0.036 boost:lagged_fibonacci607
  46. * 0.44 boost::hellekalek1995
  47. * 0.44 boost::ecuyer1988
  48. * 0.042 boost::rand48
  49. * 0.043 boost::mt11213b
  50. * 0.028 stdlib random()
  51. * 0.05 stdlib lrand48()
  52. * 0.034 stdlib rand()
  53. * 0.020 the old & lame LLRand
  54. */
  55. /**
  56. *@brief Generate a float from [0, RAND_MAX).
  57. */
  58. S32 LL_COMMON_API ll_rand();
  59. /**
  60. *@brief Generate a float from [0, val) or (val, 0].
  61. */
  62. S32 LL_COMMON_API ll_rand(S32 val);
  63. /**
  64. *@brief Generate a float from [0, 1.0).
  65. */
  66. F32 LL_COMMON_API ll_frand();
  67. /**
  68. *@brief Generate a float from [0, val) or (val, 0].
  69. */
  70. F32 LL_COMMON_API ll_frand(F32 val);
  71. /**
  72. *@brief Generate a double from [0, 1.0).
  73. */
  74. F64 LL_COMMON_API ll_drand();
  75. /**
  76. *@brief Generate a double from [0, val) or (val, 0].
  77. */
  78. F64 LL_COMMON_API ll_drand(F64 val);
  79. /**
  80. * @brief typedefs for good boost lagged fibonacci.
  81. * @see boost::lagged_fibonacci
  82. *
  83. * These generators will quickly generate doubles. Note the memory
  84. * requirements, because they are somewhat high. I chose the smallest
  85. * one, and one comparable in speed but higher periodicity without
  86. * outrageous memory requirements.
  87. * To use:
  88. * LLRandLagFib607 foo((U32)time(NULL));
  89. * double bar = foo();
  90. */
  91. typedef boost::lagged_fibonacci607 LLRandLagFib607;
  92. /**<
  93. * lengh of cycle: 2^32,000
  94. * memory: 607*sizeof(double) (about 5K)
  95. */
  96. typedef boost::lagged_fibonacci2281 LLRandLagFib2281;
  97. /**<
  98. * lengh of cycle: 2^120,000
  99. * memory: 2281*sizeof(double) (about 17K)
  100. */
  101. /**
  102. * @breif typedefs for a good boost mersenne twister implementation.
  103. * @see boost::mersenne_twister
  104. *
  105. * This fairly quickly generates U32 values
  106. * To use:
  107. * LLRandMT19937 foo((U32)time(NULL));
  108. * U32 bar = foo();
  109. *
  110. * lengh of cycle: 2^19,937-1
  111. * memory: about 2496 bytes
  112. */
  113. typedef boost::mt11213b LLRandMT19937;
  114. #endif