summaryrefslogtreecommitdiff
path: root/src/SimpleRandomizer.cc
diff options
context:
space:
mode:
authorTatsuhiro Tsujikawa <tatsuhiro.t@gmail.com>2008-09-02 14:11:59 +0000
committerTatsuhiro Tsujikawa <tatsuhiro.t@gmail.com>2008-09-02 14:11:59 +0000
commit7cbe5a79346df9c045ac640e48f711610a4852ca (patch)
tree7ca572f3ffd4d1d0131189373bd2faaa1cf60a72 /src/SimpleRandomizer.cc
parent9f5ed15e6f3a934c23ae636dac4c150d5e1d15e5 (diff)
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
Diffstat (limited to 'src/SimpleRandomizer.cc')
-rw-r--r--src/SimpleRandomizer.cc38
1 files changed, 37 insertions, 1 deletions
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 <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