blob: eca97bc8194310697757de5e57fec82661c6ca6c [file] [log] [blame]
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001//===-------------------------- random.cpp --------------------------------===//
2//
Howard Hinnantf5256e12010-05-11 21:36:01 +00003// The LLVM Compiler Infrastructure
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004//
Howard Hinnantb64f8b02010-11-16 22:09:02 +00005// This file is dual licensed under the MIT and the University of Illinois Open
6// Source Licenses. See LICENSE.TXT for details.
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00007//
8//===----------------------------------------------------------------------===//
9
10#include "random"
11#include "system_error"
12
13#include <fcntl.h>
14#include <unistd.h>
15#include <errno.h>
16
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000017_LIBCPP_BEGIN_NAMESPACE_STD
18
19random_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
26random_device::~random_device()
27{
28 close(__f_);
29}
30
31unsigned
32random_device::operator()()
33{
34 unsigned r;
35 read(__f_, &r, sizeof(r));
36 return r;
37}
38
39double
40random_device::entropy() const
41{
42 return 0;
43}
44
45_LIBCPP_END_NAMESPACE_STD