PageRenderTime 28ms CodeModel.GetById 17ms app.highlight 6ms RepoModel.GetById 1ms 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
  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
 27#ifndef LL_LLRAND_H
 28#define LL_LLRAND_H
 29
 30#include <boost/random/lagged_fibonacci.hpp>
 31#include <boost/random/mersenne_twister.hpp>
 32
 33/**
 34 * Use the boost random number generators if you want a stateful
 35 * random numbers. If you want more random numbers, use the
 36 * c-functions since they will generate faster/better randomness
 37 * across the process.
 38 *
 39 * I tested some of the boost random engines, and picked a good double
 40 * generator and a good integer generator. I also took some timings
 41 * for them on linux using gcc 3.3.5. The harness also did some other
 42 * fairly trivial operations to try to limit compiler optimizations,
 43 * so these numbers are only good for relative comparisons.
 44 *
 45 * usec/inter		algorithm
 46 * 0.21				boost::minstd_rand0
 47 * 0.039			boost:lagged_fibonacci19937
 48 * 0.036			boost:lagged_fibonacci607
 49 * 0.44				boost::hellekalek1995
 50 * 0.44				boost::ecuyer1988
 51 * 0.042			boost::rand48
 52 * 0.043			boost::mt11213b
 53 * 0.028			stdlib random() 
 54 * 0.05				stdlib lrand48()
 55 * 0.034			stdlib rand()
 56 * 0.020			the old & lame LLRand
 57 */
 58
 59/**
 60 *@brief Generate a float from [0, RAND_MAX).
 61 */
 62S32 LL_COMMON_API ll_rand();
 63
 64/**
 65 *@brief Generate a float from [0, val) or (val, 0].
 66 */
 67S32 LL_COMMON_API ll_rand(S32 val);
 68
 69/**
 70 *@brief Generate a float from [0, 1.0).
 71 */
 72F32 LL_COMMON_API ll_frand();
 73
 74/**
 75 *@brief Generate a float from [0, val) or (val, 0].
 76 */
 77F32 LL_COMMON_API ll_frand(F32 val);
 78
 79/**
 80 *@brief Generate a double from [0, 1.0).
 81 */
 82F64 LL_COMMON_API ll_drand();
 83
 84/**
 85 *@brief Generate a double from [0, val) or (val, 0].
 86 */
 87F64 LL_COMMON_API ll_drand(F64 val);
 88
 89/**
 90 * @brief typedefs for good boost lagged fibonacci.
 91 * @see boost::lagged_fibonacci
 92 *
 93 * These generators will quickly generate doubles. Note the memory
 94 * requirements, because they are somewhat high. I chose the smallest
 95 * one, and one comparable in speed but higher periodicity without
 96 * outrageous memory requirements.
 97 * To use:
 98 *  LLRandLagFib607 foo((U32)time(NULL));
 99 *  double bar = foo();
100 */
101
102typedef boost::lagged_fibonacci607 LLRandLagFib607;
103/**< 
104 * lengh of cycle: 2^32,000
105 * memory: 607*sizeof(double) (about 5K)
106 */
107
108typedef boost::lagged_fibonacci2281 LLRandLagFib2281;
109/**< 
110 * lengh of cycle: 2^120,000
111 * memory: 2281*sizeof(double) (about 17K)
112 */
113
114/**
115 * @breif typedefs for a good boost mersenne twister implementation.
116 * @see boost::mersenne_twister
117 *
118 * This fairly quickly generates U32 values
119 * To use:
120 *  LLRandMT19937 foo((U32)time(NULL));
121 *  U32 bar = foo();
122 *
123 * lengh of cycle: 2^19,937-1
124 * memory: about 2496 bytes
125 */
126typedef boost::mt11213b LLRandMT19937;
127#endif