(svn r1950) Fix: A slight adjustment in the DoRandom() function which however causes dramatic improvement in the distribution of random numbers.
authorpasky
Sun, 06 Mar 2005 22:28:35 +0000
changeset 1446 c95d3cb58985
parent 1445 038e13fa2ef6
child 1447 8300000ac36c
(svn r1950) Fix: A slight adjustment in the DoRandom() function which however causes dramatic improvement in the distribution of random numbers.

With the original generator, e.g. Random()%44 on 100 attempts regularily
generated only 22 different values! With this adjustment, the distribution is
much more uniform, almost approaching the glibc's random() call.

This means the random numbers are actually usable i.e. for the town names
generators which almost never tried a large part of the names while others were
very frequent.

Note that we attempted to test whether the randomness did not degrade in some
other way, and it came clean from the tests we performed. If you spot anything
fishy, try without this patch, though.
misc.c
--- a/misc.c	Sun Mar 06 21:20:34 2005 +0000
+++ b/misc.c	Sun Mar 06 22:28:35 2005 +0000
@@ -49,20 +49,20 @@
 	if (_current_player>=MAX_PLAYERS || !_networking) {
 		s = _random_seeds[0][0];
 		t = _random_seeds[0][1];
-		_random_seeds[0][0] = s + ROR(t ^ 0x1234567F, 7);
-		return _random_seeds[0][1] = ROR(s, 3);
+		_random_seeds[0][0] = s + ROR(t ^ 0x1234567F, 7) + 1;
+		return _random_seeds[0][1] = ROR(s, 3) - 1;
 	} else {
 		uint32 s = _player_seeds[_current_player][0];
 		uint32 t = _player_seeds[_current_player][1];
-		_player_seeds[_current_player][0] = s + ROR(t ^ 0x1234567F, 7);
+		_player_seeds[_current_player][0] = s + ROR(t ^ 0x1234567F, 7) + 1;
 		DEBUG(net, 1)("[NET-Seeds] Player seed called!");
-		return _player_seeds[_current_player][1] = ROR(s, 3);
+		return _player_seeds[_current_player][1] = ROR(s, 3) - 1;
 	}
 #else
 	s = _random_seeds[0][0];
 	t = _random_seeds[0][1];
-	_random_seeds[0][0] = s + ROR(t ^ 0x1234567F, 7);
-	return _random_seeds[0][1] = ROR(s, 3);
+	_random_seeds[0][0] = s + ROR(t ^ 0x1234567F, 7) + 1;
+	return _random_seeds[0][1] = ROR(s, 3) - 1;
 #endif
 }
 
@@ -82,8 +82,8 @@
 {
 	uint32 t = _random_seeds[1][1];
 	uint32 s = _random_seeds[1][0];
-	_random_seeds[1][0] = s + ROR(t ^ 0x1234567F, 7);
-	return _random_seeds[1][1] = ROR(s, 3);
+	_random_seeds[1][0] = s + ROR(t ^ 0x1234567F, 7) + 1;
+	return _random_seeds[1][1] = ROR(s, 3) - 1;
 }
 
 uint InteractiveRandomRange(uint max)