From 27c51e073d812364d4f769adf90c232a8817942c Mon Sep 17 00:00:00 2001 From: Marko Lindqvist Date: Fri, 28 Oct 2022 20:31:33 +0300 Subject: [PATCH 27/27] rand.c: Improve coding style See osdn #45919 Signed-off-by: Marko Lindqvist --- utility/rand.c | 70 +++++++++++++++++++++++++------------------------- 1 file changed, 35 insertions(+), 35 deletions(-) diff --git a/utility/rand.c b/utility/rand.c index 0590b0044a..963df61f5f 100644 --- a/utility/rand.c +++ b/utility/rand.c @@ -12,21 +12,21 @@ ***********************************************************************/ /************************************************************************* - The following random number generator can be found in _The Art of + The following random number generator can be found in _The Art of Computer Programming Vol 2._ (2nd ed) by Donald E. Knuth. (C) 1998. The algorithm is described in section 3.2.2 as Mitchell and Moore's - variant of a standard additive number generator. Note that the - the constants 55 and 24 are not random. Please become familiar with + variant of a standard additive number generator. Note that the + the constants 55 and 24 are not random. Please become familiar with this algorithm before you mess with it. Since the additive number generator requires a table of numbers from - which to generate its random sequences, we must invent a way to - populate that table from a single seed value. I have chosen to do - this with a different PRNG, known as the "linear congruential method" - (also found in Knuth, Vol2). I must admit that my choices of constants + which to generate its random sequences, we must invent a way to + populate that table from a single seed value. I have chosen to do + this with a different PRNG, known as the "linear congruential method" + (also found in Knuth, Vol2). I must admit that my choices of constants (3, 257, and MAX_UINT32) are probably not optimal, but they seem to work well enough for our purposes. - + Original author for this code: Cedric Tefft Modified to use rand_state struct by David Pfitzner *************************************************************************/ @@ -57,29 +57,29 @@ static RANDOM_STATE rand_state; This means that if size <= 1 the function will always return 0. Once we calculate new_rand below uniform (we hope) between 0 and - MAX_UINT32 inclusive, need to reduce to required range. Using + MAX_UINT32 inclusive, need to reduce to required range. Using modulus is bad because generators like this are generally less random for their low-significance bits, so this can give poor - results when 'size' is small. Instead want to divide the range + results when 'size' is small. Instead want to divide the range 0..MAX_UINT32 into (size) blocks, each with (divisor) values, and for any remainder, repeat the calculation of new_rand. Then: - return_val = new_rand / divisor; + return_val = new_rand / divisor; Will repeat for new_rand > max, where: max = size * divisor - 1 Then max <= MAX_UINT32 implies - size * divisor <= (MAX_UINT32+1) - thus divisor <= (MAX_UINT32+1)/size + size * divisor <= (MAX_UINT32 + 1) + thus divisor <= (MAX_UINT32 + 1) / size - Need to calculate this divisor. Want divisor as large as possible + Need to calculate this divisor. Want divisor as large as possible given above contraint, but it doesn't hurt us too much if it is a - bit smaller (just have to repeat more often). Calculation exactly - as above is complicated by fact that (MAX_UINT32+1) may not be + bit smaller (just have to repeat more often). Calculation exactly + as above is complicated by fact that (MAX_UINT32 + 1) may not be directly representable in type RANDOM_TYPE, so we do instead: - divisor = MAX_UINT32/size + divisor = MAX_UINT32 / size *************************************************************************/ RANDOM_TYPE fc_rand_debug(RANDOM_TYPE size, const char *called_as, - int line, const char *file) + int line, const char *file) { RANDOM_TYPE new_rand, divisor, max; int bailout = 0; @@ -110,7 +110,7 @@ RANDOM_TYPE fc_rand_debug(RANDOM_TYPE size, const char *called_as, rand_state.v[rand_state.x] = new_rand; if (++bailout > 10000) { - log_error("%s(%lu) = %lu bailout at %s:%d", + log_error("%s(%lu) = %lu bailout at %s:%d", called_as, (unsigned long) size, (unsigned long) new_rand, file, line); new_rand = 0; @@ -137,7 +137,7 @@ RANDOM_TYPE fc_rand_debug(RANDOM_TYPE size, const char *called_as, *************************************************************************/ void fc_srand(RANDOM_TYPE seed) { - int i; + int i; rand_state.v[0] = (seed & MAX_UINT32); @@ -145,15 +145,15 @@ void fc_srand(RANDOM_TYPE seed) rand_state.v[i] = (3 * rand_state.v[i-1] + 257) & MAX_UINT32; } - rand_state.j = (55-55); - rand_state.k = (55-24); - rand_state.x = (55-0); + rand_state.j = (55 - 55); + rand_state.k = (55 - 24); + rand_state.x = (55 - 0); rand_state.is_init = TRUE; /* Heat it up a bit: * Using modulus in fc_rand() this was important to pass - * test_random1(). Now using divisor in fc_rand() that particular + * test_random1(). Now using divisor in fc_rand() that particular * test no longer indicates problems, but this seems a good idea * anyway -- eg, other tests could well reveal other initial * problems even using divisor. @@ -235,18 +235,18 @@ void test_random1(int n) int behaviourchange = 0, behavioursame = 0; saved_state = fc_rand_state(); - /* fc_srand(time(NULL)); */ /* use current state */ + /* fc_srand(time(NULL)); */ /* Use current state */ - for (i = 0; i < n+2; i++) { + for (i = 0; i < n + 2; i++) { new_value = fc_rand(2); - if (i > 0) { /* have old */ + if (i > 0) { /* Have old */ didchange = (new_value != old_value); - if (i > 1) { /* have olddidchange */ - if (didchange != olddidchange) { - behaviourchange++; - } else { - behavioursame++; - } + if (i > 1) { /* Have olddidchange */ + if (didchange != olddidchange) { + behaviourchange++; + } else { + behavioursame++; + } } olddidchange = didchange; } @@ -255,13 +255,13 @@ void test_random1(int n) log_test("test_random1(%d) same: %d, change: %d", n, behavioursame, behaviourchange); - /* restore state: */ + /* Restore state: */ fc_rand_set_state(saved_state); } /*********************************************************************//** Local pseudo-random function for repeatedly reaching the same result, - instead of fc_rand(). Primarily needed for tiles. + instead of fc_rand(). Primarily needed for tiles. Use an invariant equation for seed. Result is 0 to (size - 1). -- 2.35.1