blob: 5af8f4649729944378899dac359025bc4fbef258 [file] [log] [blame]
Howard Hinnantc51e1022010-05-11 19:42:16 +00001//===-------------------------- random.cpp --------------------------------===//
2//
Howard Hinnantc566dc32010-05-11 21:36:01 +00003// The LLVM Compiler Infrastructure
Howard Hinnantc51e1022010-05-11 19:42:16 +00004//
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
Howard Hinnantc51e1022010-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