libzypp  17.25.0
Random.cc
Go to the documentation of this file.
1 #include <fcntl.h>
2 #include <unistd.h>
3 #include <zypp/base/Random.h>
4 
5 
6 namespace zypp { namespace base {
7 
8  // Taken from KApplication
9 int random_int()
10 {
11  static bool init = false;
12  if (!init)
13  {
14  unsigned int seed;
15  init = true;
16  int fd = open("/dev/urandom", O_RDONLY|O_CLOEXEC);
17  if (fd < 0 || ::read(fd, &seed, sizeof(seed)) != sizeof(seed))
18  {
19  // No /dev/urandom... try something else.
20  srand(getpid());
21  seed = rand()+time(0);
22  }
23  if (fd >= 0) close(fd);
24  srand(seed);
25  }
26  return rand();
27 }
28 
29 // Taken from KApplication
30 std::string random_string(int length)
31 {
32  if (length <=0 ) return std::string();
33 
34  std::string str; str.resize( length );
35  int i = 0;
36  while (length--)
37  {
38  int r=::random() % 62;
39  r+=48;
40  if (r>57) r+=7;
41  if (r>90) r+=6;
42  str[i++] = char(r);
43  // so what if I work backwards?
44  }
45  return str;
46 }
47 
48 
49 } }
50 
zypp::base::sysconfig::read
std::map< std::string, std::string > read(const Pathname &_path)
Read sysconfig file path_r and return (key,valye) pairs.
Definition: Sysconfig.cc:34
zypp::base::random
unsigned random()
Return a random number from [0,RAND_MAX[.
Definition: Random.h:28
zypp::base::random_int
int random_int()
Definition: Random.cc:9
zypp
Easy-to use interface to the ZYPP dependency resolver.
Definition: CodePitfalls.doc:1
Random.h
zypp::base::random_string
std::string random_string(int length)
Definition: Random.cc:30
str
String related utilities and Regular expression matching.