Generates a random number from [0, range), using a Linear Congruential Generator (LCG). Crashes if 'range' is 0 or greater than kMaxRange.
| 1585 | // Congruential Generator (LCG). Crashes if 'range' is 0 or greater |
| 1586 | // than kMaxRange. |
| 1587 | UInt32 Random::Generate(UInt32 range) { |
| 1588 | // These constants are the same as are used in glibc's rand(3). |
| 1589 | state_ = (1103515245U*state_ + 12345U) % kMaxRange; |
| 1590 | |
| 1591 | GTEST_CHECK_(range > 0) |
| 1592 | << "Cannot generate a number in the range [0, 0)."; |
| 1593 | GTEST_CHECK_(range <= kMaxRange) |
| 1594 | << "Generation of a number in [0, " << range << ") was requested, " |
| 1595 | << "but this can only generate numbers in [0, " << kMaxRange << ")."; |
| 1596 | |
| 1597 | // Converting via modulus introduces a bit of downward bias, but |
| 1598 | // it's simple, and a linear congruential generator isn't too good |
| 1599 | // to begin with. |
| 1600 | return state_ % range; |
| 1601 | } |
| 1602 | |
| 1603 | // GTestIsInitialized() returns true iff the user has initialized |
| 1604 | // Google Test. Useful for catching the user mistake of not initializing |