forked from speed-dreams/speed-dreams-code
Re #784 New -n/--norandom cli switch for 'repeatable randomness' (srand never called in this case) + removed CMake option OPTION_REPEATABLE_RANDOM
git-svn-id: https://svn.code.sf.net/p/speed-dreams/code/trunk@5240 30fe4595-0a0c-4342-8851-515496e4dcbd Former-commit-id: 0c9df0fd9590265e45e18cbd5cc1d0b053d047a7 Former-commit-id: d8faa7b0605ba4c2c80ddc4eec30de8ef9245734
This commit is contained in:
parent
84ea2fc8d5
commit
cd448beb0c
5 changed files with 28 additions and 15 deletions
|
@ -58,9 +58,6 @@ MACRO(ADD_SD_COMPILE_OPTIONS)
|
|||
|
||||
SET(OPTION_TRACE_LEVEL "5" CACHE STRING "Trace level integer threshold, only if OPTION_TRACE (traces with higher level are not logged ; 0=Fatal, 1=Error, 2=Warning, 3=Info, 4=Trace, 5=Debug, ...)")
|
||||
|
||||
SET(OPTION_REPEATABLE_RANDOM false CACHE BOOL "Not for production use ; if On, enables repeatable random processes (srand is called multiple times)")
|
||||
MARK_AS_ADVANCED(OPTION_REPEATABLE_RANDOM)
|
||||
|
||||
SET(OPTION_PROFILER false CACHE BOOL "Enable profiler")
|
||||
|
||||
SET(OPTION_SCHEDULE_SPY false CACHE BOOL "Enable fine grained scheduling spy")
|
||||
|
@ -129,10 +126,6 @@ MACRO(ADD_SD_COMPILE_OPTIONS)
|
|||
ADD_DEFINITIONS(-DTRACE_LEVEL=${OPTION_TRACE_LEVEL})
|
||||
ENDIF(OPTION_TRACE_LEVEL)
|
||||
|
||||
IF(OPTION_REPEATABLE_RANDOM)
|
||||
ADD_DEFINITIONS(-DREPEATABLE_RANDOM)
|
||||
ENDIF(OPTION_REPEATABLE_RANDOM)
|
||||
|
||||
IF(OPTION_XRANDR)
|
||||
ADD_DEFINITIONS(-DUSE_RANDR_EXT)
|
||||
ENDIF(OPTION_XRANDR)
|
||||
|
|
|
@ -21,7 +21,7 @@ static const float PI = 3.1415927f;
|
|||
|
||||
void setRandomSeed(unsigned int seed)
|
||||
{
|
||||
//srand(seed); // Already done in tgf::gfInit (needed only once in the process life time)
|
||||
srand(seed);
|
||||
}
|
||||
|
||||
real urandom()
|
||||
|
|
|
@ -23,6 +23,7 @@
|
|||
*/
|
||||
|
||||
#include <cerrno>
|
||||
#include <ctime>
|
||||
#include <limits>
|
||||
#include <iostream>
|
||||
|
||||
|
@ -95,6 +96,7 @@ void GfApplication::initialize(bool bLoggingEnabled, int argc, char **argv)
|
|||
registerOption("D", "datadir", /* nHasValue = */ true);
|
||||
registerOption("e", "tracelevel", /* nHasValue = */ true);
|
||||
registerOption("t", "tracestream", /* nHasValue = */ true);
|
||||
registerOption("n", "norandom", /* nHasValue = */ false);
|
||||
|
||||
// Help about the command line options.
|
||||
addOptionsHelpSyntaxLine("[-l|--localdir <dir path>] [-L|--libdir <dir path>]");
|
||||
|
@ -104,6 +106,7 @@ void GfApplication::initialize(bool bLoggingEnabled, int argc, char **argv)
|
|||
" [-t|--tracestream stdout|stderr|<file name>]");
|
||||
#endif
|
||||
addOptionsHelpSyntaxLine("[-v|--version]");
|
||||
addOptionsHelpSyntaxLine("[-n|--norandom]");
|
||||
|
||||
addOptionsHelpExplainLine
|
||||
("- locadir : Root dir of the tree where user settings files are stored");
|
||||
|
@ -123,11 +126,13 @@ void GfApplication::initialize(bool bLoggingEnabled, int argc, char **argv)
|
|||
(" (default=" SD_DATADIR ")");
|
||||
#ifdef TRACE_OUT
|
||||
addOptionsHelpExplainLine
|
||||
("- tracelevel : Maximum level of displayed traces");
|
||||
("- tracelevel : Maximum level of displayed traces for the default logger");
|
||||
addOptionsHelpExplainLine
|
||||
(" (0=Fatal, 1=Error, 2=Warning, 3=Info, 4=Trace, 5=Debug, ... ; default=5)");
|
||||
addOptionsHelpExplainLine
|
||||
("- tracestream : Target output stream for the traces (default=stderr)");
|
||||
("- tracestream : Target output stream for the default logger (default=stderr)");
|
||||
addOptionsHelpExplainLine
|
||||
("- norandom : Force reproducible random sequences for every game session (default=off)");
|
||||
#endif
|
||||
}
|
||||
|
||||
|
@ -319,6 +324,8 @@ bool GfApplication::parseOptions()
|
|||
int nDefTraceLevel = std::numeric_limits<int>::min();
|
||||
std::string strDefTraceStream;
|
||||
|
||||
bool bTrueRandom = true;
|
||||
|
||||
std::list<Option>::const_iterator itOpt;
|
||||
for (itOpt = _lstOptions.begin(); itOpt != _lstOptions.end(); itOpt++)
|
||||
{
|
||||
|
@ -372,6 +379,11 @@ bool GfApplication::parseOptions()
|
|||
{
|
||||
strDefTraceStream = itOpt->strValue;
|
||||
}
|
||||
// Initialize random generator or not.
|
||||
else if (itOpt->strLongName == "norandom")
|
||||
{
|
||||
bTrueRandom = false;
|
||||
}
|
||||
else
|
||||
{
|
||||
// If we get here, this is normal : the derived classes might have declared
|
||||
|
@ -411,6 +423,19 @@ bool GfApplication::parseOptions()
|
|||
GfLogDefault.setLevelThreshold(nDefTraceLevel);
|
||||
if (!strDefTraceStream.empty())
|
||||
GfLogDefault.setStream(strDefTraceStream);
|
||||
|
||||
|
||||
// Initialize random generator with "random" seed, or not (=> always same rand() sequence).
|
||||
if (bTrueRandom)
|
||||
{
|
||||
GfLogInfo("Initializing random number generator for 'true randomness'\n");
|
||||
srand((unsigned)time(0));
|
||||
}
|
||||
// Note: Never calling srand is equivalent to calling it once with seed=1.
|
||||
else
|
||||
{
|
||||
GfLogInfo("Not initializing random number generator, for 'repeatable randomness'\n");
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
|
|
@ -27,7 +27,6 @@
|
|||
|
||||
#include <cstdio>
|
||||
#include <cerrno>
|
||||
#include <ctime>
|
||||
#include <cmath>
|
||||
#include <cstring>
|
||||
|
||||
|
@ -453,9 +452,6 @@ void GfInit(bool bWithLogging)
|
|||
|
||||
gfParamInit();
|
||||
|
||||
// Initialize random generator.
|
||||
srand((unsigned)time(NULL));
|
||||
|
||||
// Initialize SDL and useful subsystems (some others may be initialized in tgfclient).
|
||||
if (SDL_Init(SDL_INIT_TIMER) < 0)
|
||||
GfLogFatal("Couldn't initialize SDL(timer) (%s)\n", SDL_GetError());
|
||||
|
|
|
@ -513,7 +513,6 @@ void ReCareerNew()
|
|||
|
||||
t = time(NULL);
|
||||
stm = localtime(&t);
|
||||
//srand((unsigned int)t); // Already done in tgf::gfInit (needed only once in the process life time)
|
||||
snprintf( buf, 1024, "%sresults/%s/%%s-%4d-%02d-%02d-%02d-%02d%%s%%s%%s.xml%%s", GfLocalDir(), ReInfo->_reFilename,
|
||||
stm->tm_year + 1900, stm->tm_mon + 1, stm->tm_mday, stm->tm_hour, stm->tm_min );
|
||||
filename = strdup(buf); // Makes it possible to reuse buf
|
||||
|
|
Loading…
Reference in a new issue