-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrand.cpp
More file actions
46 lines (41 loc) · 884 Bytes
/
Copy pathrand.cpp
File metadata and controls
46 lines (41 loc) · 884 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#include <iostream>
#include <cstdlib>
#include <climits>
#include <cassert>
#ifdef __cplusplus
extern "C" {
#endif
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <error.h>
#include <errno.h>
#ifdef __cplusplus
}
#endif
#include "rand.h"
using namespace std;
Rand::Rand(unsigned long type)
{
if(type & RAND_TYPE_RANDOM)
_type = RAND_TYPE_RANDOM;
else
_type = RAND_TYPE_PSEUDO_RANDOM;
if(_type & RAND_TYPE_RANDOM)
_fd = open("/dev/random", O_RDONLY);
else
_fd = open("/dev/urandom", O_RDONLY);
if(_fd == -1)
error_at_line(1, errno, __FILE__, __LINE__, "file open failed.");
}
Rand::~Rand(void)
{
close(_fd);
}
void Rand::getRand(void *data, int len)
{
int ret = read(_fd, data, len);
if(ret == -1)
error_at_line(1, errno, __FILE__, __LINE__, "file read failed.");
}