From 9f27068fb7fdace77ca2643dbfaf0abc7d6d3379 Mon Sep 17 00:00:00 2001 From: auria Date: Sun, 19 Dec 2010 01:55:29 +0000 Subject: [PATCH] Fixed kart paths randomness issue (and probably many others) : don't continuously call srand, srand is made to be called ONCE only, repeatedly calling srand gives very poor randomisation git-svn-id: svn+ssh://svn.code.sf.net/p/supertuxkart/code/main/trunk@7082 178a84e3-b1eb-0310-8ba1-8eac791a3b58 --- src/karts/controller/ai_base_controller.cpp | 12 +++++++++--- src/karts/controller/new_ai_controller.cpp | 2 -- src/karts/kart_properties_manager.cpp | 1 - src/main.cpp | 2 ++ src/utils/random_generator.cpp | 1 - 5 files changed, 11 insertions(+), 7 deletions(-) diff --git a/src/karts/controller/ai_base_controller.cpp b/src/karts/controller/ai_base_controller.cpp index 1898e03ad..cba6d42e8 100644 --- a/src/karts/controller/ai_base_controller.cpp +++ b/src/karts/controller/ai_base_controller.cpp @@ -44,7 +44,7 @@ AIBaseController::AIBaseController(Kart *kart, m_next_node_index.reserve(m_quad_graph->getNumNodes()); m_successor_index.reserve(m_quad_graph->getNumNodes()); std::vector next; - + for(unsigned int i=0; igetNumNodes(); i++) { next.clear(); @@ -52,12 +52,18 @@ AIBaseController::AIBaseController(Kart *kart, // For now pick one part on random, which is not adjusted during the // race. Long term statistics might be gathered to determine the // best way, potentially depending on race position etc. - int indx = rand() % next.size(); + int r = rand(); + int indx = (int)( + (r / (float)(RAND_MAX - 1)) * next.size() + ); + + if (next.size() > 1) + printf("Next road : %i mapped to %i -> %i\n", r, (int)next.size(), indx); m_successor_index.push_back(indx); m_next_node_index.push_back(next[indx]); } - + const unsigned int look_ahead=10; // Now compute for each node in the graph the list of the next 'look_ahead' // graph nodes. This is the list of node that is tested in checkCrashes. diff --git a/src/karts/controller/new_ai_controller.cpp b/src/karts/controller/new_ai_controller.cpp index b0ae23371..d0d6e8ec3 100644 --- a/src/karts/controller/new_ai_controller.cpp +++ b/src/karts/controller/new_ai_controller.cpp @@ -548,8 +548,6 @@ void NewAIController::handleRaceStart() //5% in medium and less than 1% of the karts in hard. if( m_time_till_start < 0.0f ) { - srand(( unsigned ) time( 0 )); - //Each kart starts at a different, random time, and the time is //smaller depending on the difficulty. m_time_till_start = ( float ) rand() / RAND_MAX * m_max_start_delay; diff --git a/src/karts/kart_properties_manager.cpp b/src/karts/kart_properties_manager.cpp index ba2978ab2..8a9460e82 100644 --- a/src/karts/kart_properties_manager.cpp +++ b/src/karts/kart_properties_manager.cpp @@ -366,7 +366,6 @@ std::vector KartPropertiesManager::getRandomKartList(int count, else i++; } - std::srand((unsigned int)std::time(0)); std::random_shuffle(karts.begin(), karts.end()); // Loop over all karts to fill till either all slots are filled, or diff --git a/src/main.cpp b/src/main.cpp index 68e4c399e..cde1a8933 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -639,6 +639,8 @@ void cleanTuxKart() int main(int argc, char *argv[] ) { + srand(( unsigned ) time( 0 )); + try { // Init the minimum managers so that user config exists, then // handle all command line options that do not need (or must diff --git a/src/utils/random_generator.cpp b/src/utils/random_generator.cpp index 293b3b32f..f2829e80e 100644 --- a/src/utils/random_generator.cpp +++ b/src/utils/random_generator.cpp @@ -30,7 +30,6 @@ RandomGenerator::RandomGenerator() m_c = 12345; m_all_random_generators.push_back(this); m_random_value = 3141591; - std::srand((unsigned int)std::time(0)); } // RandomGenerator // ----------------------------------------------------------------------------