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>
Contact tracker frequently when the number of connections are 0 and

View File

@ -33,9 +33,45 @@
*/
/* copyright --> */
#include "SimpleRandomizer.h"
#include "a2time.h"
#include <cstdlib>
#include <sys/types.h>
#include <unistd.h>
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

View File

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