---------------------------------------------------------------------------------- Description: A cheap thread-safe lock-free pseudo random number generator based on http://www.firstpr.com.au/dsp/rand31/ Return Value: Random integer between 0 and 2^31. ----------------------------------------------------------------------------------
| 143 | // Random integer between 0 and 2^31. |
| 144 | // ---------------------------------------------------------------------------------- |
| 145 | static inline uint32_t RandCheap() |
| 146 | { |
| 147 | static CcpAtomic<uint32_t> globalSeed( rand() ); |
| 148 | // We increment global seed here so that if two threads get to this point |
| 149 | // simultaneously they would still get different results. |
| 150 | uint32_t seed = globalSeed; |
| 151 | uint32_t hi, lo; |
| 152 | lo = 16807 * ( seed & 0xffff ); |
| 153 | hi = 16807 * ( seed >> 16 ); |
| 154 | lo += ( hi & 0x7fff ) << 16; |
| 155 | lo += hi >> 15; |
| 156 | lo = ( lo & 0x7FFFFFFF ) + ( lo >> 31 ); |
| 157 | globalSeed = static_cast<uint32_t>( lo ); |
| 158 | return lo; |
| 159 | } |
| 160 | |
| 161 | // ---------------------------------------------------------------------------------- |
| 162 | // Description: |