libzypp: zypp/base/Random.cc Source File

libzypp 17.36.6

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