xref: /llvm-project/libcxx/src/random.cpp (revision 5b08a8a43254ed30bd953e869b0fd9fc1e8b82d0)
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 
18 _LIBCPP_BEGIN_NAMESPACE_STD
19 
20 random_device::random_device(const string& __token)
21     : __f_(open(__token.c_str(), O_RDONLY))
22 {
23     if (__f_ <= 0)
24         __throw_system_error(errno, ("random_device failed to open " + __token).c_str());
25 }
26 
27 random_device::~random_device()
28 {
29     close(__f_);
30 }
31 
32 unsigned
33 random_device::operator()()
34 {
35     unsigned r;
36     read(__f_, &r, sizeof(r));
37     return r;
38 }
39 
40 double
41 random_device::entropy() const
42 {
43     return 0;
44 }
45 
46 _LIBCPP_END_NAMESPACE_STD
47