Generates a random number from [0, range), using a Linear Congruential Generator (LCG). Crashes if 'range' is 0 or greater than kMaxRange.
| 1773 | // Congruential Generator (LCG). Crashes if 'range' is 0 or greater |
| 1774 | // than kMaxRange. |
| 1775 | UInt32 Random::Generate(UInt32 range) { |
| 1776 | // These constants are the same as are used in glibc's rand(3). |
| 1777 | state_ = (1103515245U*state_ + 12345U) % kMaxRange; |
| 1778 | |
| 1779 | GTEST_CHECK_(range > 0) |
| 1780 | << "Cannot generate a number in the range [0, 0)."; |
| 1781 | GTEST_CHECK_(range <= kMaxRange) |
| 1782 | << "Generation of a number in [0, " << range << ") was requested, " |
| 1783 | << "but this can only generate numbers in [0, " << kMaxRange << ")."; |
| 1784 | |
| 1785 | // Converting via modulus introduces a bit of downward bias, but |
| 1786 | // it's simple, and a linear congruential generator isn't too good |
| 1787 | // to begin with. |
| 1788 | return state_ % range; |
| 1789 | } |
| 1790 | |
| 1791 | // GTestIsInitialized() returns true iff the user has initialized |
| 1792 | // Google Test. Useful for catching the user mistake of not initializing |