From 38fd655af90f64e92dd14261eca72943ce162f29 Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Sat, 13 Mar 2021 08:14:01 +0100 Subject: [PATCH] remove the weird rounding macro from peasant growth --- src/laws.c | 4 ++-- src/util/rng.h | 1 - src/util/rng.test.c | 22 +++++++--------------- 3 files changed, 9 insertions(+), 18 deletions(-) diff --git a/src/laws.c b/src/laws.c index 9db1027a9..ffdbde1d0 100644 --- a/src/laws.c +++ b/src/laws.c @@ -334,7 +334,7 @@ int peasant_luck_effect(int peasants, int luck, int maxp, double variance) mean *= peasant_luck_factor() * peasant_growth_factor(); mean *= ((peasants / (double)maxp < .9) ? 1 : PEASANTFORCE); - births = RAND_ROUND(normalvariate(mean, variance * mean)); + births = (int)ceil(normalvariate(mean, variance * mean)); if (births <= 0) births = 1; if (births > peasants / 2) @@ -353,7 +353,7 @@ static void peasants(region * r, int rule) if (peasants > 0 && rule > 0) { int luck = 0; double fraction = peasants * peasant_growth_factor(); - int births = RAND_ROUND(fraction); + int births = (int)ceil(fraction); attrib *a = a_find(r->attribs, &at_peasantluck); if (a != NULL) { diff --git a/src/util/rng.h b/src/util/rng.h index 42ebc5c57..5c51677bf 100644 --- a/src/util/rng.h +++ b/src/util/rng.h @@ -25,7 +25,6 @@ extern "C" { # define rng_double ((rand()%RAND_MAX)/(double)RAND_MAX) # define RNG_RAND_MAX RAND_MAX #endif -#define RAND_ROUND(fractional) ((rng_double() < fractional-(int)fractional)?((int)fractional+1):((int)fractional)) #ifdef __cplusplus } #endif diff --git a/src/util/rng.test.c b/src/util/rng.test.c index ebe8bd124..0d88eaa78 100644 --- a/src/util/rng.test.c +++ b/src/util/rng.test.c @@ -5,22 +5,15 @@ #include #include -static void test_rng_round(CuTest * tc) -{ - double f, r; - - test_setup(); - f = rng_double(); - r = RAND_ROUND(f); - CuAssertTrue(tc, f >= 0); - CuAssertTrue(tc, r <= f + 1); - CuAssertTrue(tc, r >= f); - CuAssertTrue(tc, r == r); - CuAssertTrue(tc, r == RAND_ROUND(r)); -} - static void test_dice_rand(CuTest* tc) { + test_setup(); + + random_source_inject_constants(0.0, 0); + CuAssertIntEquals(tc, 1, dice_rand("1d10")); + CuAssertIntEquals(tc, 1, dice_rand("d20")); + CuAssertIntEquals(tc, 2, dice_rand("2d4")); + CuAssertIntEquals(tc, 9, dice_rand("3*(2+1)")); CuAssertIntEquals(tc, 0, dice_rand("0")); CuAssertIntEquals(tc, -5, dice_rand("-5")); @@ -34,7 +27,6 @@ static void test_dice_rand(CuTest* tc) CuSuite *get_rng_suite(void) { CuSuite *suite = CuSuiteNew(); - SUITE_ADD_TEST(suite, test_rng_round); SUITE_ADD_TEST(suite, test_dice_rand); return suite; }