From 5bde040295f3c50da86fbe18d61bacc71d8e436a Mon Sep 17 00:00:00 2001 From: Jonas 'Sortie' Termansen Date: Thu, 17 Nov 2011 13:11:09 +0100 Subject: [PATCH] Fixed randomness-related crash in snake. --- games/snake.cpp | 10 ++++++---- libmaxsi/random.cpp | 4 ++-- 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/games/snake.cpp b/games/snake.cpp index c1ac17e1..bea1690e 100644 --- a/games/snake.cpp +++ b/games/snake.cpp @@ -56,8 +56,8 @@ void Reset() case 3: velx = 0; vely = -1; break; } - animalx = 2 + (rand() % width-4); - animaly = 2 + (rand() % height-4); + animalx = 2 + (rand() % (width-4)); + animaly = 2 + (rand() % (height-4)); taillen = 0; tailmax = 3; @@ -141,8 +141,10 @@ void Update() if ( newx == animalx && newy == animaly ) { tailmax++; - animalx = 2 + (rand() % width-4); - animaly = 2 + (rand() % height-4); + animalx = 2 + (rand() % (width-4)); + animaly = 2 + (rand() % (height-4)); + ASSERT(0 <= animalx && animalx < width); + ASSERT(0 <= animaly && animaly < height); if ( maxspeed < speed ) { speed += speedincrease; } } diff --git a/libmaxsi/random.cpp b/libmaxsi/random.cpp index 63daa8a4..9bbef8de 100644 --- a/libmaxsi/random.cpp +++ b/libmaxsi/random.cpp @@ -28,11 +28,11 @@ namespace Maxsi { namespace Random { - int random_seed=1337; + unsigned random_seed = 1337; extern "C" int rand() { random_seed = random_seed + 37 * 1103515245 + 12345; - return random_seed / 65536; + return random_seed >> 16; } } }