2008-09-02 Tatsuhiro Tsujikawa <tujikawa at rednoah dot com>

Moved implementation to SimpleRandomizer.cc from 
SimpleRandomizer.h.
	Added return value of getpid() to argument of srand() to achieve 
more
	randomized value.
	* src/SimpleRandomizer.cc
	* src/SimpleRandomizer.h
pull/1/head
Tatsuhiro Tsujikawa 2008-09-02 14:11:59 +00:00
parent 9f5ed15e6f
commit 7cbe5a7934
3 changed files with 54 additions and 26 deletions

View File

@ -1,3 +1,11 @@
2008-09-02 Tatsuhiro Tsujikawa <tujikawa at rednoah dot com>
Moved implementation to SimpleRandomizer.cc from SimpleRandomizer.h.
Added return value of getpid() to argument of srand() to achieve more
randomized value.
* src/SimpleRandomizer.cc
* src/SimpleRandomizer.h
2008-09-02 Tatsuhiro Tsujikawa <tujikawa at rednoah dot com> 2008-09-02 Tatsuhiro Tsujikawa <tujikawa at rednoah dot com>
Contact tracker frequently when the number of connections are 0 and Contact tracker frequently when the number of connections are 0 and

View File

@ -33,9 +33,45 @@
*/ */
/* copyright --> */ /* copyright --> */
#include "SimpleRandomizer.h" #include "SimpleRandomizer.h"
#include "a2time.h"
#include <cstdlib>
#include <sys/types.h>
#include <unistd.h>
namespace aria2 { namespace aria2 {
RandomizerHandle SimpleRandomizer::randomizer; SharedHandle<Randomizer> SimpleRandomizer::_randomizer;
SharedHandle<Randomizer> SimpleRandomizer::getInstance()
{
if(_randomizer.isNull()) {
_randomizer.reset(new SimpleRandomizer());
}
return _randomizer;
}
void SimpleRandomizer::init()
{
srand(time(0)^getpid());
}
SimpleRandomizer::SimpleRandomizer() {}
SimpleRandomizer::~SimpleRandomizer() {}
long int SimpleRandomizer::getRandomNumber()
{
return rand();
}
long int SimpleRandomizer::getMaxRandomNumber()
{
return RAND_MAX;
}
long int SimpleRandomizer::getRandomNumber(long int to)
{
return(int32_t)(((double)to)*getRandomNumber()/(getMaxRandomNumber()+1.0));
}
} // namespace aria2 } // namespace aria2

View File

@ -36,46 +36,30 @@
#define _D_SIMPLE_RANDOMIZER_H_ #define _D_SIMPLE_RANDOMIZER_H_
#include "Randomizer.h" #include "Randomizer.h"
#include "a2time.h"
#include <stdlib.h>
namespace aria2 { namespace aria2 {
class SimpleRandomizer : public Randomizer { class SimpleRandomizer : public Randomizer {
private: private:
static RandomizerHandle randomizer; static SharedHandle<Randomizer> _randomizer;
SimpleRandomizer() {} SimpleRandomizer();
public: public:
static RandomizerHandle getInstance() { static SharedHandle<Randomizer> getInstance();
if(randomizer.isNull()) {
randomizer.reset(new SimpleRandomizer());
}
return randomizer;
}
static void init() { static void init();
srand(time(0));
}
virtual ~SimpleRandomizer() {} virtual ~SimpleRandomizer();
virtual long int getRandomNumber() { virtual long int getRandomNumber();
return rand();
}
virtual long int getMaxRandomNumber() { virtual long int getMaxRandomNumber();
return RAND_MAX;
}
/** /**
* Returns random number in [0, to). * Returns random number in [0, to).
*/ */
virtual long int getRandomNumber(long int to) virtual long int getRandomNumber(long int to);
{
return(int32_t)(((double)to)*getRandomNumber()/(getMaxRandomNumber()+1.0));
}
}; };
} // namespace aria2 } // namespace aria2