From 7cbe5a79346df9c045ac640e48f711610a4852ca Mon Sep 17 00:00:00 2001 From: Tatsuhiro Tsujikawa Date: Tue, 2 Sep 2008 14:11:59 +0000 Subject: 2008-09-02 Tatsuhiro Tsujikawa 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 --- src/SimpleRandomizer.cc | 38 +++++++++++++++++++++++++++++++++++++- 1 file changed, 37 insertions(+), 1 deletion(-) (limited to 'src/SimpleRandomizer.cc') diff --git a/src/SimpleRandomizer.cc b/src/SimpleRandomizer.cc index 28dce73b..760dafb5 100644 --- a/src/SimpleRandomizer.cc +++ b/src/SimpleRandomizer.cc @@ -33,9 +33,45 @@ */ /* copyright --> */ #include "SimpleRandomizer.h" +#include "a2time.h" +#include +#include +#include namespace aria2 { -RandomizerHandle SimpleRandomizer::randomizer; +SharedHandle SimpleRandomizer::_randomizer; + +SharedHandle 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 -- cgit