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