← Back to team overview

widelands-dev team mailing list archive

[Merge] lp:~widelands-dev/widelands/fix-random-tribe into lp:widelands

 

Shevonar has proposed merging lp:~widelands-dev/widelands/fix-random-tribe into lp:widelands.

Requested reviews:
  Tino (tino79)
Related bugs:
  Bug #1089731 in widelands: "random tribe always gives Empire"
  https://bugs.launchpad.net/widelands/+bug/1089731
  Bug #1302635 in widelands: "Random tribe selection always gives the same result"
  https://bugs.launchpad.net/widelands/+bug/1302635

For more details, see:
https://code.launchpad.net/~widelands-dev/widelands/fix-random-tribe/+merge/214451

Proper seeding of std::rand function before selecting random tribe. I am not sure if the time function is available on Windows like this, therefore I would like Tino to test this before merging. Thanks :)
-- 
https://code.launchpad.net/~widelands-dev/widelands/fix-random-tribe/+merge/214451
Your team Widelands Developers is subscribed to branch lp:~widelands-dev/widelands/fix-random-tribe.
=== modified file 'src/network/nethost.cc'
--- src/network/nethost.cc	2014-03-29 16:04:47 +0000
+++ src/network/nethost.cc	2014-04-06 20:34:23 +0000
@@ -186,6 +186,7 @@
 							newstate = PlayerSettings::stateClosed;
 					}
 				} else if (it == impls.end()) {
+					std::srand(time(nullptr));
 					do {
 						uint8_t random = (std::rand() % impls.size()); // Choose a random AI
 						it = impls.begin() + random;
@@ -1642,6 +1643,7 @@
 
 	if (random_tribe) {
 		uint8_t num_tribes = d->settings.tribes.size();
+		std::srand(time(nullptr));
 		uint8_t random = (std::rand() % num_tribes);
 		actual_tribe = d->settings.tribes.at(random).name;
 	}

=== modified file 'src/single_player_game_settings_provider.cc'
--- src/single_player_game_settings_provider.cc	2014-03-25 07:39:28 +0000
+++ src/single_player_game_settings_provider.cc	2014-04-06 20:34:23 +0000
@@ -136,6 +136,7 @@
 			it = impls.begin();
 		} else if (it == impls.end()) {
 			s.players[number].random_ai = true;
+			std::srand(time(nullptr));
 			do {
 				uint8_t random = (std::rand() % impls.size()); // Choose a random AI
 				it = impls.begin() + random;
@@ -158,6 +159,7 @@
 
 	if (random_tribe) {
 		uint8_t num_tribes = s.tribes.size();
+		std::srand(time(nullptr));
 		uint8_t random = (std::rand() % num_tribes);
 		actual_tribe = s.tribes.at(random).name;
 	}


Follow ups