--- parser3/src/classes/math.C 2001/07/07 16:38:01 1.3 +++ parser3/src/classes/math.C 2001/10/19 12:43:29 1.7 @@ -2,10 +2,10 @@ Parser: @b math parser class. Copyright (c) 2001 ArtLebedev Group (http://www.artlebedev.com) - Author: Alexander Petrosyan (http://design.ru/paf) + + $Id: math.C,v 1.7 2001/10/19 12:43:29 parser Exp $ */ -static const char *RCSId="$Id: math.C,v 1.3 2001/07/07 16:38:01 parser Exp $"; #include "pa_config_includes.h" #include "pa_common.h" @@ -13,6 +13,10 @@ static const char *RCSId="$Id: math.C,v #include "pa_vmath.h" #include "pa_request.h" +#ifdef WIN32 +# include +#endif + // defines #define PI 3.1415926535 @@ -23,23 +27,28 @@ static const char *RCSId="$Id: math.C,v class MMath : public Methoded { public: MMath(Pool& pool); + void configure_admin(Request& r); + public: // Methoded bool used_directly() { return true; } }; // methods - +static unsigned int randomizer=0; static void _random(Request& r, const String& method_name, MethodParams *params) { Pool& pool=r.pool(); Value& range=params->as_junction(0, "range must be expression"); uint max=(uint)r.process(range).as_double(); if(max<=1) - PTHROW(0, 0, + throw Exception(0, 0, &method_name, "bad range [0..%u]", max); - r.write_no_lang(*new(pool) VInt(pool, rand()%max)); + Value& result=*new(pool) VInt(pool, (int)( + ((double)((randomizer=rand())% RAND_MAX)) / RAND_MAX * (max + 1))); + result.set_name(method_name); + r.write_no_lang(result); } @@ -106,10 +115,6 @@ MATH2(pow); MMath::MMath(Pool& apool) : Methoded(apool) { set_name(*NEW String(pool(), MATH_CLASS_NAME)); - - - // setting seed - srand(getpid()+time(NULL)); rand(); // ^FUNC(expr) #define ADD1(name) \ @@ -134,6 +139,24 @@ MMath::MMath(Pool& apool) : Methoded(apo } +// in MSVC each thread has it's own pseudo-random sequence +// in win32 apache each thread can handle multiple requests +// so to get proper randoms we remember random generated in one thread +void MMath::configure_admin(Request&) { + // setting seed + srand( + randomizer ^ +#ifdef WIN32 + GetCurrentThreadId() ^ +#else + getpid() ^ +#endif + (unsigned int)time(NULL) + ); + if(!randomizer) + randomizer=rand(); +} + // global variables Methoded *math_base_class;