xref: /llvm-project/libcxx/src/random.cpp (revision 940e211c87ceb6e43bf7a2bfd5df9783ad75ae6c)
1 //===-------------------------- random.cpp --------------------------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 
10 #include "random"
11 #include "system_error"
12 
13 #include <fcntl.h>
14 #include <unistd.h>
15 #include <errno.h>
16 
17 _LIBCPP_BEGIN_NAMESPACE_STD
18 
19 random_device::random_device(const string& __token)
20     : __f_(open(__token.c_str(), O_RDONLY))
21 {
22     if (__f_ <= 0)
23         __throw_system_error(errno, ("random_device failed to open " + __token).c_str());
24 }
25 
26 random_device::~random_device()
27 {
28     close(__f_);
29 }
30 
31 unsigned
32 random_device::operator()()
33 {
34     unsigned r;
35     read(__f_, &r, sizeof(r));
36     return r;
37 }
38 
39 double
40 random_device::entropy() const
41 {
42     return 0;
43 }
44 
45 _LIBCPP_END_NAMESPACE_STD
46