enhanced base/Random.h; duplicate code removed
authorMichael Andres <ma@suse.de>
Mon, 18 Jun 2007 20:55:26 +0000 (20:55 +0000)
committerMichael Andres <ma@suse.de>
Mon, 18 Jun 2007 20:55:26 +0000 (20:55 +0000)
zypp/base/Random.cc
zypp/base/Random.h
zypp/source/SourceImpl.cc

index e6abfe5..123da94 100644 (file)
@@ -39,7 +39,7 @@ std::string random_string(int length)
   int i = 0;
   while (length--)
   {
-      int r=random() % 62;
+      int r=::random() % 62;
       r+=48;
       if (r>57) r+=7;
       if (r>90) r+=6;
@@ -48,7 +48,7 @@ std::string random_string(int length)
   }
   return str;
 }
-  
-  
+
+
 } }
 
index 6daf788..9c988f9 100644 (file)
@@ -23,6 +23,24 @@ namespace zypp
     // Taken from KApplication
     std::string random_string(int length);
 
+
+    /** Return a random number from <tt>[0,RAND_MAX[</tt>. */
+    inline unsigned random()
+    {
+      return random_int();
+    }
+    /** Return a random number from <tt>[0,size_r[</tt>. */
+    inline unsigned random( unsigned size_r )
+    {
+      return random_int() % size_r;
+    }
+    /** Return a random number from <tt>[min_r,min_r+size_r[</tt>. */
+    inline unsigned random( unsigned min_r, unsigned size_r )
+    {
+      return min_r + random( size_r );
+    }
+
+
   } //ns base
 } // ns zypp
 
index 3a24538..350ee35 100644 (file)
@@ -13,6 +13,7 @@
 #include <fstream>
 #include "zypp/base/Logger.h"
 #include "zypp/base/Gettext.h"
+#include "zypp/base/Random.h"
 #include "zypp/Digest.h"
 #include "zypp/SourceFactory.h"
 #include "zypp/source/SourceImpl.h"
@@ -32,48 +33,6 @@ namespace source
 
 IMPL_PTR_TYPE(SourceImpl);
 
-// Taken from KApplication
-static int random()
-{
-  static bool init = false;
-  if (!init)
-  {
-    unsigned int seed;
-    init = true;
-    int fd = open("/dev/urandom", O_RDONLY);
-    if (fd < 0 || ::read(fd, &seed, sizeof(seed)) != sizeof(seed))
-    {
-      // No /dev/urandom... try something else.
-      srand(getpid());
-      seed = rand()+time(0);
-    }
-    if (fd >= 0) close(fd);
-    srand(seed);
-  }
-  return rand();
-}
-
-// Taken from KApplication
-static std::string randomString(int length)
-{
-  if (length <=0 ) return std::string();
-
-  std::string str;
-  str.resize( length );
-  int i = 0;
-  while (length--)
-  {
-    int r=random() % 62;
-    r+=48;
-    if (r>57) r+=7;
-    if (r>90) r+=6;
-    str[i++] =  char(r);
-    // so what if I work backwards?
-  }
-  return str;
-}
-
-
 class DownloadProgressPackageReceiver : public callback::ReceiveReport<media::DownloadProgressReport>
 {
   callback::SendReport <DownloadResolvableReport> & _report;