Line data Source code
1 : /* Copyright (C) 2025 Wildfire Games. 2 : * 3 : * Permission is hereby granted, free of charge, to any person obtaining 4 : * a copy of this software and associated documentation files (the 5 : * "Software"), to deal in the Software without restriction, including 6 : * without limitation the rights to use, copy, modify, merge, publish, 7 : * distribute, sublicense, and/or sell copies of the Software, and to 8 : * permit persons to whom the Software is furnished to do so, subject to 9 : * the following conditions: 10 : * 11 : * The above copyright notice and this permission notice shall be included 12 : * in all copies or substantial portions of the Software. 13 : * 14 : * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 15 : * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 16 : * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 17 : * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY 18 : * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, 19 : * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE 20 : * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 21 : */ 22 : 23 : /* 24 : * pseudorandom number generator 25 : */ 26 : 27 : #include "precompiled.h" 28 : 29 : #include "rand.h" 30 : 31 : #include "lib/debug.h" 32 : #include "lib/status.h" 33 : 34 : #include <cstdlib> 35 : 36 : // avoids several common pitfalls; see discussion at 37 : // http://www.azillionmonkeys.com/qed/random.html 38 : 39 : // rand() is poorly implemented (e.g. in VC7) and only returns < 16 bits; 40 : // double that amount by concatenating 2 random numbers. 41 : // this is not to fix poor rand() randomness - the number returned will be 42 : // folded down to a much smaller interval anyway. instead, a larger XRAND_MAX 43 : // decreases the probability of having to repeat the loop. 44 : #if RAND_MAX < 65536 45 : static const size_t XRAND_MAX = (RAND_MAX+1)*(RAND_MAX+1) - 1; 46 : static size_t xrand() 47 : { 48 : return rand()*(RAND_MAX+1) + rand(); 49 : } 50 : // rand() is already ok; no need to do anything. 51 : #else 52 : static const size_t XRAND_MAX = RAND_MAX; 53 222 : static size_t xrand() 54 : { 55 444 : return rand(); 56 : } 57 : #endif 58 : 59 204 : size_t rand(size_t min_inclusive, size_t max_exclusive) 60 : { 61 204 : const size_t range = (max_exclusive-min_inclusive); 62 : // huge interval or min >= max 63 204 : if(range == 0 || range > XRAND_MAX) 64 : { 65 2 : WARN_IF_ERR(ERR::INVALID_PARAM); 66 2 : return 0; 67 : } 68 : 69 202 : const size_t inv_range = XRAND_MAX / range; 70 : 71 : // generate random number in [0, range) 72 : // idea: avoid skewed distributions when <range> doesn't evenly divide 73 : // XRAND_MAX by simply discarding values in the "remainder". 74 : // not expected to run often since XRAND_MAX is large. 75 222 : size_t x; 76 222 : do 77 : { 78 222 : x = xrand(); 79 : } 80 222 : while(x >= range * inv_range); 81 202 : x /= inv_range; 82 : 83 202 : x += min_inclusive; 84 202 : ENSURE(x < max_exclusive); 85 : return x; 86 : }