From 076bb8908bf76e25e9178c178321a7f690b002d8 Mon Sep 17 00:00:00 2001 From: NHOrus Date: Mon, 11 Sep 2017 21:20:46 +0300 Subject: [PATCH] If divident negative, then remainder is negative too. RNG values need to be always positive. Solution: Transposing positively by divisor. In all the two places it may happen. --- misc.c | 6 ++++-- saveresume.c | 5 +++++ 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/misc.c b/misc.c index 302e61c..80e4352 100644 --- a/misc.c +++ b/misc.c @@ -646,8 +646,10 @@ bool tstbit(long mask, int bit) void set_seed(int32_t seedval) /* Set the LCG seed */ { - game.lcg_x = (uint32_t) seedval % LCG_M; - + game.lcg_x = seedval % LCG_M; + if (game.lcg_x < 0) { + game.lcg_x = LCG_M + game.lcg_x; + } // once seed is set, we need to generate the Z`ZZZ word for (int i = 0; i < 5; ++i) { game.zzword[i] = 'A' + randrange(26); diff --git a/saveresume.c b/saveresume.c index f1d52d4..8bd4418 100644 --- a/saveresume.c +++ b/saveresume.c @@ -145,6 +145,11 @@ bool is_valid(struct game_t* valgame) valgame->lcg_x %= LCG_M; } + /* Check for RNG underflow. Transpose */ + if (valgame->lcg_x < LCG_M) { + valgame->lcg_x = LCG_M + (valgame->lcg_x % LCG_M); + } + /* Bounds check for locations */ if ( valgame->chloc < -1 || valgame->chloc > NLOCATIONS || valgame->chloc2 < -1 || valgame->chloc2 > NLOCATIONS || -- 2.31.1